Android来电、去电监听
2013-07-29 11:10
232 查看
Android手机中添加手机来电的状态,使用PhoneStateListener来监听。
上面的MyPhoneStateListener是一个内部类,继承PhoneStateListener。
class EncryptPhoneStateListener extends PhoneStateListener {
EncryptPhoneStateListener() {
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
}
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:// 拨打电话或者接听电话时
McLog.i("state = CALL_STATE_OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:// 电话进来时
McLog.i("state = CALL_STATE_RINGING");
break;
case TelephonyManager.CALL_STATE_IDLE:// 挂起电话时候,或者没有任何反映
McLog.i("state = CALL_STATE_IDLE");
break;
default:
break;
}
}
}
onCallStateChanged就是一个回调方法,incomingNumber就是来电的号码。
以上方法只适合监听来电,不适合去电,如要监听去电,需要在上面方法中添加额外的东西。
android.intent.action.NEW_OUTGOING_CALL需要定义一个广播接收器,接受的动作是android.intent.action.NEW_OUTGOING_CALL。
public class MainReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) { // 去电
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
}
在intent中就传递了号码,同时结合上面的电话状态,判断去电什么时候接通和挂断。
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE); // 开始监听 PhoneStateListener phoneListener = new MyPhoneStateListener(); telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); // 取消监听 telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_NONE);
上面的MyPhoneStateListener是一个内部类,继承PhoneStateListener。
class EncryptPhoneStateListener extends PhoneStateListener {
EncryptPhoneStateListener() {
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
}
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:// 拨打电话或者接听电话时
McLog.i("state = CALL_STATE_OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:// 电话进来时
McLog.i("state = CALL_STATE_RINGING");
break;
case TelephonyManager.CALL_STATE_IDLE:// 挂起电话时候,或者没有任何反映
McLog.i("state = CALL_STATE_IDLE");
break;
default:
break;
}
}
}
onCallStateChanged就是一个回调方法,incomingNumber就是来电的号码。
以上方法只适合监听来电,不适合去电,如要监听去电,需要在上面方法中添加额外的东西。
android.intent.action.NEW_OUTGOING_CALL需要定义一个广播接收器,接受的动作是android.intent.action.NEW_OUTGOING_CALL。
public class MainReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) { // 去电
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
}
在intent中就传递了号码,同时结合上面的电话状态,判断去电什么时候接通和挂断。
相关文章推荐
- android 电话状态的监听(来电和去电)
- Android监听器实现(二)Broadcast方式对通话状态(来电,拨号,挂机)的精确监听
- android监听来电去电广播
- Android监听来电和去电
- android 呼入电话的监听(来电监听)
- Android监听系统来电并弹出提示窗口
- android 监听电话去电接通瞬间[非来电]
- android-轻松监听来电和去电
- android 电话状态的监听(来电和去电)
- Android应用监听来电、短信等方法
- android 电话状态监听(来电和去电)实现代码
- android如何使用BroadcastReceiver后台实现来电通话记录的监听并存取到sqllite数据库通过Contentprovilder实现接口
- Android初学习 - 获取手机状态和监听手机来电状态
- android源码开发之监听来电状态
- android 拨打电话、 监听来电、监听呼出电话的功能实现
- Android 监听来电和去电状态
- android-轻松监听来电和去电
- android 2.1 监听电话状态并自动接听来电
- 如何实现android中监听来电并生成悬浮窗体提示
- Android来电监听