我需要在
时识别广播事件外出通话已开始,
接收方收到的外出电话和
外出通话挂断
我可以接收来电事件,但不能接听电话事件。
这是我的代码,请找出我的问题并给出解决方案。
先谢谢
@Override
public void onReceive(Context context, Intent intent) {
// We listen to two intents. The new outgoing call only tells us of an
// outgoing call. We use it to get the number.
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras()
.getString("android.intent.extra.PHONE_NUMBER");
} else {
String stateStr = intent.getExtras()
.getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras()
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, number);
}
}
答案 0 :(得分:0)
以下代码可帮助您获取onCreate
中的网络工作状态代码 StateListener phoneStateListener = new StateListener();
TelephonyManager telephonyManager =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
为手机的收听状态创建一个内部类。
class StateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("call Activity off hook");
LockScreenActivity.this.finish();
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
在清单文件中设置权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
答案 1 :(得分:0)
1)外出通话已经开始,
您可以识别是否在未使用以下代码
的情况下启动拨出电话 case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallReceived(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
startRecording(context,"_Out_",number);
onOutgoingCallStarted(context, savedNumber, callStartTime);
}
else
{
isIncoming = true;
callStartTime = new Date();
startRecording(context,"_In_",savedNumber);
onIncomingCallAnswered(context, number, callStartTime);
}
break;
2)接收端收到的外出呼叫和 3)外出通话挂断
然而你无法识别通过使用任何直接api被其他端拾取的呼叫,因为android没有提供任何直接api来检测它。