第一步:建立後臺監聽來電服務AddressService 並在功能清單文件註冊;android
所需權限:數據庫
<uses-permission android:name="android.permission.READ_PHONE_STATE" />app
AddressService 代碼以下:ide
package com.zaizai.safty.service; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.PixelFormat; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import android.widget.Toast; import com.zaizai.safty.R; import com.zaizai.safty.db.dao.NumberAddressQueryUtils; public class AddressService extends Service { /** * 電話 */ private TelephonyManager telephonyManager; private MyListenerPhone myListenerPhone; private class MyListenerPhone extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { // state:狀態,incomingNumber:來電號碼 super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING:// 來電鈴聲響起 // 查詢數據庫的操做 String address = NumberAddressQueryUtils .queryNumber(incomingNumber); Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG).show(); // myToast(address); break; case TelephonyManager.CALL_STATE_IDLE://電話的空閒狀態:掛電話、來電拒絕 break; default: break; } } } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); //電話服務 telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // 監聽來電 myListenerPhone = new MyListenerPhone(); //註冊服務去監聽電話 telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_CALL_STATE); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); // 取消監聽來電 telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_NONE); myListenerPhone = null; } }
第二步:在AddressService 服務裏面註冊來電狀態(TelephoneManager)佈局
//電話服務 telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // 監聽來電 myListenerPhone = new MyListenerPhone(); //註冊服務去監聽電話 telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_CALL_STATE);
三、自定義監聽類MyPhoneStateListenerui
private class MyListenerPhone extends PhoneStateListener {
@Overridethis
* 當呼叫狀態發生改變的時候回調該方法
public void onCallStateChanged(int state, String incomingNumber) {
// state:狀態,incomingNumber:來電號碼
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:// 來電鈴聲響起
// 查詢數據庫的操做
String address = NumberAddressQueryUtils
.queryNumber(incomingNumber);
Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG).show();
// myToast(address);
break;
case TelephonyManager.CALL_STATE_IDLE://電話的空閒狀態:掛電話、來電拒絕
break;
default:
break;
}
}
}
spa
 code
AddressDao.getAddress(incomingNumber);orm
在onDestroy()被調用時取消監聽:
@Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); // 取消監聽來電 telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_NONE); myListenerPhone = null;
四、在SettingActivity設置中內心配置設置,當點擊開啓的時候就啓動服務,不然相反。而後演示;
佈局文件:
<com.itheima.mobilesafe.ui.SettingItemView
android:id="@+id/siv_show_address"
itheima:title="設置號碼歸屬地顯示"
itheima:desc_on="號碼歸屬地顯示已經打開"
itheima:desc_off="號碼歸屬地顯示已經關閉"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
SettingActivity裏的代碼:
//設置號碼歸屬地顯示
siv_show_address = (SettingItemView) findViewById(R.id.siv_show_address);
showAddressIntent = new Intent(this, AddressService.class);
siv_show_address.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(siv_show_address.isCheck()){
siv_show_address.setCheck(false);
stopService(showAddressIntent);
//sp共享偏好去保存
}else{
startService(showAddressIntent);
siv_show_address.setCheck(true);
}
}
});
五、建立一個類ServiceStatusUtils 裏面的方法isServiceRunning()校驗檢查一個服務是否開啓;
/**
* 檢驗一個服務是否還運行,系統全部的
*/
public static boolean isServiceRunning(Context context,String serviceName){
ActivityManager am= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> infos = am.getRunningServices(100);
for(RunningServiceInfo info : infos){
String className = info.service.getClassName();
if(className.equals(serviceName)){
return true;
}
}
return false;
}