继续N天前的项目
开启服务监听手机来电,查询数据库,显示归属地
详细内容可以参考这篇博文:http://www.cnblogs.com/taoshihan/p/5331232.html
AddressService.java
package com.qingguow.mobilesafe.service;import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.widget.Toast;/*** 来电显示* * @author taoshihan* */ public class AddressService extends Service {private TelephonyManager tm;private MyPhoneStateListener phoneStateListener;@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}/*** 服务创建*/@Overridepublic void onCreate() {super.onCreate();tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);phoneStateListener = new MyPhoneStateListener();tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);}private class MyPhoneStateListener extends PhoneStateListener {@Overridepublic void onCallStateChanged(int state, String incomingNumber) {super.onCallStateChanged(state, incomingNumber);switch (state) {case TelephonyManager.CALL_STATE_RINGING:String info = NumberQueryAddressUtil.queryAddress(incomingNumber);Toast.makeText(getApplicationContext(), info, 1).show();break;default:break;}}}/*** 服务销毁*/@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();//取消监听 tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);phoneStateListener=null;} }
设置中心,配置是否开启来电归属地显示
直接使用我们之前定义好的组合控件
<com.qingguow.mobilesafe.ui.SettingItemViewtsh:title="设置显示号码归属地"tsh:desc_on="设置显示号码归属地已开启"tsh:desc_off="设置显示号码归属地已关闭"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/siv_show_address"></com.qingguow.mobilesafe.ui.SettingItemView>
获取到SettingItemView对象,我们自定义的控件,设置状态
调用SettingItemView对象的setOnClickListener()方法,设置点击事件,重写onClick方法
调用SettingItemView对象的isChecked()方法,得到当前是否选中
判断状态,调用SettingItemView对象的setChecked()方法,设置状态,参数:布尔值
调用startService()方法,开启监听手机状态的服务,参数:Intent对象,
调用stopService()方法,关闭服务
判断当前服务是否开启,设置控件的默认选中状态
新建一个工具类ServicesUtils.java
定义一个静态方法isServiceRunning(),传入参数:Context上下文,String服务名
调用Context对象的getSystemService()方法,获取ActivityManager对象,参数:Context.ACTIVITY_SERVICE
调用ActivityManager对象的getRunningServices()方法,得到运行的服务List集合,参数:int最大值
for循环List集合,每条是个RunningServiceInfo对象
调用RunningServiceInfo.servie.getClassName(),获取到String服务类名,判断一下如果相等返回true
SettingActivity.java
package com.qingguow.mobilesafe;import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;import com.qingguow.mobilesafe.service.AddressService; import com.qingguow.mobilesafe.ui.SettingItemView; import com.qingguow.mobilesafe.utils.ServiceUtils;public class SettingActivity extends Activity {private SettingItemView siv_item;private SharedPreferences sp;// 设置是否开启号码归属地private SettingItemView showAddressBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_setting);// 设置号码归属地showAddressBtn = (SettingItemView) findViewById(R.id.siv_show_address);if (ServiceUtils.isRunningService(this,"com.qingguow.mobilesafe.service.AddressService")) {showAddressBtn.setChecked(true);} else {showAddressBtn.setChecked(false);}showAddressBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {if (showAddressBtn.isChecked()) {showAddressBtn.setChecked(false);stopService(new Intent(getApplicationContext(),AddressService.class));} else {showAddressBtn.setChecked(true);startService(new Intent(getApplicationContext(),AddressService.class));}}});siv_item = (SettingItemView) findViewById(R.id.siv_item);sp = getSharedPreferences("config", MODE_PRIVATE);// 根据保存的数据设置状态boolean update = sp.getBoolean("update", false);if (update) {siv_item.setChecked(true);} else {siv_item.setChecked(false);}// 自动更新的点击事件siv_item.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Editor editor = sp.edit();if (siv_item.isChecked()) {// 设置不选中siv_item.setChecked(false);editor.putBoolean("update", false);} else {// 设置选中siv_item.setChecked(true);editor.putBoolean("update", true);}editor.commit();}});} }
ServicesUtils.java
package com.qingguow.mobilesafe.utils;import java.util.List;import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.content.Context; /*** 服务工具类* @author taoshihan**/ public class ServiceUtils {/*** 判断某服务是否开启* @param context* @param serviceName* @return*/public static boolean isRunningService(Context context,String serviceName){ActivityManager am=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);List<RunningServiceInfo> infos=am.getRunningServices(100);for(RunningServiceInfo info:infos){String name=info.service.getClassName();if(name.equals(serviceName)){return true;}}return false;} }