安卓获取来去电号码

2019-04-14 21:38发布

第一步,写一个类继承于BroadCastRecriver, 第二步,注册要监听的动作 第三步,添加需要权限
一,java实现代码 public class PhoneStateBroadCastReceiver extends BroadcastReceiver { TelephonyManager tm; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub tm=(TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE); //去电监听 if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){ Log.i("huang", "jj+:"+getResultData()); //获得去电的号码 String number=getResultData(); Toast.makeText(context, NumberAddressQuery.getAddress(number), 1).show(); } //来电监听 else if(intent.getAction().equals("android.intent.action.PHONE_STATE")){ switch(tm.getCallState()){ case 1: Log.i("huang", "来电"); //获得来电的号码 String inNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); Log.i("huang","来电——:"+inNumber); Toast.makeText(context, NumberAddressQuery.getAddress(inNumber), 1).show(); break; } } } }


二,监听动作



三,权限