在来电监听服务中动态注册去电广播(BroadcastRecevier)

2019-04-13 17:03发布



1 去电广播接收函数: /** * 广播监听receiver * @author admin * */ public class OutCallRecevierDynamicRegister extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String num = getResultData(); String address = AddressDao.getAddress(num); Toast.makeText(context, address, Toast.LENGTH_LONG).show(); } }
2 在Service服务的OnCreate中动态注册广播监听: //动态注册一个广播监听 IntentFilter intentfilter =new IntentFilter(); intentfilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL); outCallRecevierDynamicRegister = new OutCallRecevierDynamicRegister(); registerReceiver(outCallRecevierDynamicRegister, intentfilter);
3 在Service服务的OnDestroy动态注销广播监听: unregisterReceiver(outCallRecevierDynamicRegister);//动态消除一个广播监听