android 来电去电监听

2019-04-14 21:36发布

package com.pocketdigi.phonelistener; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; public class PhoneReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub System.out.println("action"+intent.getAction()); if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){ //如果是去电(拨出) System.out.println("拨出"); }else{ //查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电 System.out.println("来电"); TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); //设置一个监听器 } } PhoneStateListener listener=new PhoneStateListener(){ @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub //state 当前状态 incomingNumber,貌似没有去电的API super.onCallStateChanged(state, incomingNumber); switch(state){ case TelephonyManager.CALL_STATE_IDLE: System.out.println("挂断"); break; case TelephonyManager.CALL_STATE_OFFHOOK: System.out.println("接听"); break; case TelephonyManager.CALL_STATE_RINGING: System.out.println("响铃:来电号码"+incomingNumber); //输出来电号码 break; } } }; }
要在AndroidManifest.xml注册广播接收器: 1 2 3 4 5 6 android:name=".PhoneReceiver"> android:name="android.intent.action.PHONE_STATE"/> android:name="android.intent.action.NEW_OUTGOING_CALL" /> 还要添加权限: 1 2 android:name="android.permission.READ_PHONE_STATE"> android:name="android.permission.PROCESS_OUTGOING_CALLS"> <