android 拨打电话 监听来电监听呼出电话的功能实现
Posted Mars-xq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 拨打电话 监听来电监听呼出电话的功能实现相关的知识,希望对你有一定的参考价值。
demo1(通用):
权限
<!-- 监听呼出电话 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<!-- 监听来电 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 拨打电话 -->
<uses-permission android:name="android.permission.CALL_PHONE" />
注册:
<receiver android:name=".PhoneReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
声明:
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneReceiver extends BroadcastReceiver {
private static final String TAG = PhoneReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// 如果是拨打电话
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String outNumber = this.getResultData();// 去电号码
Log.i(TAG, "call OUT 1:" + phoneNumber);
Log.i(TAG, "call OUT 2:" + outNumber);
} else if ("android.intent.action.PHONE_STATE".equals(intent.getAction())) {
// 如果是来电
TelephonyManager tManager = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
// 来电号码
String mIncomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i(TAG, "call IN 1:" + state);
Log.i(TAG, "call IN 2:" + mIncomingNumber);
switch (tManager.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "**********************监测到电话呼入!!!!*****");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "**********************监测到接听电话!!!!************");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "**********************监测到挂断电话!!!!*******************");
break;
}
}
}
}
demo2 :
//1.获取telephone的实例
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//2.注册电话的监听
tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
private class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String phoneNumber) {
super.onCallStateChanged(state, phoneNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "**********************监测到挂断电话!!!!*******************");
break;
case TelephonyManager.CALL_STATE_OFFHOOK://接听状态
Log.d(TAG, "**********************监测到接听电话!!!!************");
break;
case TelephonyManager.CALL_STATE_RINGING://电话响铃状态
Log.d(TAG, "**********************监测到电话呼入!!!!*****");
break;
default:
break;
}
}
}
以上是关于android 拨打电话 监听来电监听呼出电话的功能实现的主要内容,如果未能解决你的问题,请参考以下文章