[AndroidTips]Android监听来电和去电
2019-04-13 21:13发布
生成海报
参考:
android 呼入电话的监听(来电监听)
http://stephen830.iteye.com/blog/1181010
android 呼出电话的监听(去电监听)
http://stephen830.iteye.com/blog/1181452
android-轻松监听来电和去电
http://www.eoeandroid.com/thread-8994-1-1.html
android 呼入电话的监听(来电监听)
需要权限:
Xml代码
-
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
方式一:通过广播接收来电
定义来电广播接收类
Java代码
-
package com.zhouzijing.android.demo;
-
-
import android.content.BroadcastReceiver;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.telephony.TelephonyManager;
-
import android.util.Log;
-
-
public class BroadcastReceiverMgr extends BroadcastReceiver {
-
-
private final String TAG = MyBroadcastReceiver.TAG;
-
-
@Override
-
public void onReceive(Context context, Intent intent) {
-
String action = intent.getAction();
-
Log.i(TAG, "[Broadcast]"+action);
-
-
-
if(action.equals(MyBroadcastReceiver.B_PHONE_STATE)){
-
Log.i(TAG, "[Broadcast]PHONE_STATE");
-
doReceivePhone(context,intent);
-
}
-
}
-
-
-
-
-
-
-
public void doReceivePhone(Context context, Intent intent) {
-
String phoneNumber = intent.getStringExtra(
-
TelephonyManager.EXTRA_INCOMING_NUMBER);
-
TelephonyManager telephony =
-
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
-
int state = telephony.getCallState();
-
switch(state){
-
case TelephonyManager.CALL_STATE_RINGING:
-
Log.i(TAG, "[Broadcast]等待接电话="+phoneNumber);
-
break;
-
case TelephonyManager.CALL_STATE_IDLE:
-
Log.i(TAG, "[Broadcast]电话挂断="+phoneNumber);
-
break;
-
case TelephonyManager.CALL_STATE_OFFHOOK:
-
Log.i(TAG, "[Broadcast]通话中="+phoneNumber);
-
break;
-
}
-
}
-
}
定义Actitvity类
Java代码
-
package com.zhouzijing.android.demo;
-
-
import android.app.Activity;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.content.IntentFilter;
-
import android.os.Bundle;
-
import android.telephony.PhoneStateListener;
-
import android.telephony.TelephonyManager;
-
import android.util.Log;
-
import android.view.View;
-
-
public class MyBroadcastReceiver extends Activity {
-
public final static String TAG = "MyBroadcastReceiver";
-
-
public final static String B_PHONE_STATE =
-
TelephonyManager.ACTION_PHONE_STATE_CHANGED;
-
-
private BroadcastReceiverMgr mBroadcastReceiver;
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.my_broadcast_receiver);
-
}
-
-
-
public void registerIt(View v) {
-
Log.i(TAG, "registerIt");
-
mBroadcastReceiver = new BroadcastReceiverMgr();
-
IntentFilter intentFilter = new IntentFilter();
-
intentFilter.addAction(B_PHONE_STATE);
-
intentFilter.setPriority(Integer.MAX_VALUE);
-
registerReceiver(mBroadcastReceiver, intentFilter);
-
}
-
-
-
public void unregisterIt(View v) {
-
Log.i(TAG, "unregisterIt");
-
unregisterReceiver(mBroadcastReceiver);
-
}
-
-
}
方式二:通过监听器来实现
Java代码
-
package com.zhouzijing.android.demo;
-
-
import android.app.Activity;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.content.IntentFilter;
-
import android.os.Bundle;
-
import android.telephony.PhoneStateListener;
-
import android.telephony.TelephonyManager;
-
import android.util.Log;
-
import android.view.View;
-
-
public class MyBroadcastReceiver extends Activity {
-
public final static String TAG = "MyBroadcastReceiver";
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.my_broadcast_receiver);
-
}
-
-
-
-
-
-
public void createPhoneListener(View v) {
-
TelephonyManager telephony = (TelephonyManager)getSystemService(
-
Context.TELEPHONY_SERVICE);
-
telephony.listen(new OnePhoneStateListener(),
-
PhoneStateListener.LISTEN_CALL_STATE);
-
}
-
-
-
-
-
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮