需要权限:
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);
}
}
/**
* 处理电话广播.
* @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类
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);
}
//按钮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);
}
}
方式二:通过监听器来实现
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);
}
/**
* 按钮-监听电话
* @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);
}
}
}