参考:
android 呼入电话的监听(来电监听)
http://stephen830.iteye.com/blog/1181010
android 呼出电话的监听(去电监听)
http://stephen830.iteye.com/blog/1181452
android-轻松监听来电和去电
android 呼入电话的监听(来电监听)
需要权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
方式一:通过广播接收来电
定义来电广播接收类
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);
}
}
/**
* 处理电话广播.
* @param context
* @param 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类
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);
}
//按钮1-注册广播
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);
}
//按钮2-撤销广播
public void unregisterIt(View v) {
Log.i(TAG, "unregisterIt");
unregisterReceiver(mBroadcastReceiver);
}
}
方式二:通过监听器来实现
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);
}
/**
* 按钮-监听电话
* @param v
*/
public void createPhoneListener(View v) {
TelephonyManager telephony = (TelephonyManager)getSystemService(
Context.TELEPHONY_SERVICE);
telephony.listen(new OnePhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}
/**
* 电话状态监听.
* @author stephen
*
*/
class OnePhoneStateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(TAG, "[Listener]电话号码:"+incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG, "[Listener]等待接电话:"+incomingNumber);
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG, "[Listener]电话挂断:"+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG, "[Listener]通话中:"+incomingNumber);
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
}
android 呼出电话的监听(去电监听)
权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
通过接收呼出电话的广播来实现
定义广播类
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_ACTION_NEW_OUTGOING_CALL)){
String outPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "[Broadcast]ACTION_NEW_OUTGOING_CALL:"+outPhoneNumber);
//this.setResultData(null);
//这里可以更改呼出电话号码。如果设置为null,电话就永远不会播出了.
}
}
}
定义activity类
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_ACTION_NEW_OUTGOING_CALL = Intent.ACTION_NEW_OUTGOING_CALL;
private BroadcastReceiverMgr mBroadcastReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_broadcast_receiver);
}
//按钮1-注册广播
public void registerIt(View v) {
Log.i(TAG, "registerIt");
mBroadcastReceiver = new BroadcastReceiverMgr();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
intentFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mBroadcastReceiver, intentFilter);
}
//按钮2-撤销广播
public void unregisterIt(View v) {
Log.i(TAG, "unregisterIt");
unregisterReceiver(mBroadcastReceiver);
}
}
---------------------------------------------------------------------------------------------------------
要监听android打电话和接电话,只需下面2步骤
1.第一步,写一个Receiver继承自BroadcastReceiver
public class PhoneStatReceiver extends BroadcastReceiver{
private static final String TAG = "PhoneStatReceiver";
// private static MyPhoneStateListener phoneListener = new MyPhoneStateListener();
private static boolean incomingFlag = false;
private static String incoming_number = null;
@Override
public void onReceive(Context context, Intent intent) {
//如果是拨打电话
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
incomingFlag = false;
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "call OUT:"+phoneNumber);
}else{
//如果是来电
TelephonyManager tm =
(TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
incomingFlag = true;//标识当前是来电
incoming_number = intent.getStringExtra("incoming_number");
Log.i(TAG, "RINGING :"+ incoming_number);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(incomingFlag){
Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if(incomingFlag){
Log.i(TAG, "incoming IDLE");
}
break;
}
}
}
}
第二步:在AndroidManifest.xml,配置写好的Receiver,并拦截相应的BroadCastAction,
另外注意加上相应的权限。
<receiver android:name=".filter.PhoneStatReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>