Android 监听来电广播
Posted 小zhong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 监听来电广播相关的知识,希望对你有一定的参考价值。
一、添加监听电话状态所需权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
二、接受电话状态的广播
public class PhoneStateReceiver extends BroadcastReceiver
private static final String TAG = "PhoneStateReceiver";
public void onReceive(Context context, Intent intent)
Log.i(TAG, "onReceive()");
String action = intent.getAction();
if (action.equals(Intent.ACTION_NEW_OUTGOING_CALL)) // 去电
String outNumber = this.getResultData();// 去电号码
else if ("android.intent.action.PHONE_STATE".equals(action)) // 来电
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String inNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);// 来电号码
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) // 电话正在响铃
else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) // 挂断
else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) // 摘机,通话状态
三、在AndroidManifest.xml,配置写好的Receiver:
<receiver android:name=".PhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
以上是关于Android 监听来电广播的主要内容,如果未能解决你的问题,请参考以下文章