要实现电话窃听,需要监听电话的状态,方法如下:
/* 取得电话服务 */
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener(){
@Override public void onCallStateChanged(int state, String incomingNumber) {
switch (state){
case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */
break;
case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */
break;
case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
//监听电话的状态
telManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
在清单文件AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
// service内部类,监听拨打接听电话,如果有电话呼入,则暂停播放,通话结束,继续播放
public static class PhoneStatReceiver extends BroadcastReceiver {
private static boolean bPlayingFlg = false;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// 呼出电话
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
if (null != mMediaPlayer) {
if (mMediaPlayer.isPlaying()) {
bPlayingFlg = true;
pause(); // 暂停播放
MusicDemo.mbSeekBarUpdateFlg = false; // 停止更新播放进度
}
}
} else {
// 呼入电话
TelephonyManager tm =
(TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
// 当前是来电
if (null != mMediaPlayer) {
if (mMediaPlayer.isPlaying()) {
bPlayingFlg = true;
pause(); // 暂停播放
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE: // 挂机 Device call state: No activity.
if (bPlayingFlg) {
// 恢复播放
pauseToPlay();
MusicDemo.mbSeekBarUpdateFlg = true; // 更新播放进度
bPlayingFlg = false;
}
break;
}
}
}
}
自己写的监听电话拨出和呼入状态,完全没问题。
在manifest中增加权限<uses-permission android:name="android.permission.READ_PHONE_STATE">uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS">uses-permission>
阅读(1277) | 评论(0) | 转发(0) |