使用广播接收者接收打电话的意图,显示号码归属地
新建一个类OutCallReceiver继承系统的BroadcastReceiver
重写onReceive()方法
调用getResultData()方法,获取到String电话号码,查询数据库获取到归属地
package com.qingguow.mobilesafe.receiver; import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * 接收打电话广播 * @author taoshihan * */ public class OutcallReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { String phone=getResultData(); String address=NumberQueryAddressUtil.queryAddress(phone); System.out.println("111"+address); Toast.makeText(arg0, address, 1).show(); } }
清单文件中定义<receiver>节点,和<intent-filter>节点,设置<action>节点名称,android:name=”android.intent.action.NEW_OUTGOING_CALL”
需要权限,android.permission.PROCESS_OUTGOING_CALLS
代码注册广播
此时的广播接收者,只要一打电话就会被调用,用户体验不好,我们使用代码注册广播接收者,用设置中心的开启监听电话来电的部分来控制这个广播
在服务里面调用registerReceiver()方法,注册广播,参数:BroadcastReceiver对象,IntentFilter对象
获取IntentFilter对象,调用IntentFilter对象的addAction()方法,参数:android.intent.action.NEW_OUTGOING_CALL
在服务里面解除注册广播,调用unregisterReceiver()方法,参数:BroadcastReceiver对象
package com.qingguow.mobilesafe.service; import com.qingguow.mobilesafe.receiver.OutcallReceiver; import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil; import android.app.Service; import android.content.Intent; import android.content.IntentFilter; 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; private OutcallReceiver outcallReceiver; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } /** * 服务创建 */ @Override public void onCreate() { super.onCreate(); tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); phoneStateListener = new MyPhoneStateListener(); tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); //注册广播 outcallReceiver=new OutcallReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("android.intent.action.NEW_OUTGOING_CALL"); registerReceiver(outcallReceiver, filter); } private class MyPhoneStateListener extends PhoneStateListener { @Override public 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; } } } /** * 服务销毁 */ @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); //取消监听 tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); phoneStateListener=null; //解除注册广播 unregisterReceiver(outcallReceiver); outcallReceiver=null; } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/app/5399.html