我发现了许多问题,这些问题描述了如何使用电话号码管理器.And获取电话呼叫状态,我在stackoverflow - How to detect when phone is answered or rejected中跟踪了这个问题。但这是为了单独检测不同的手机状态。,我需要添加一个警报对话框,只有当来电挂起而没有应答。如何检测这个特定的事件。?
发布于 2015-09-22 12:16:50
Android5.0及以上版本可能会发送重复事件。你可以考虑保持以前的状态来比较-
private class PhoneStateChangeListener extends PhoneStateListener {
static final int IDLE = 0;
static final int OFFHOOK = 1;
static final int RINGING = 2;
int lastState = IDLE;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
lastState = RINGING;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
lastState = OFFHOOK;
break;
case TelephonyManager.CALL_STATE_IDLE:
if (lastState == RINGING) {
// Process for call hangup without being answered
}
lastState = IDLE;
break;
}
}
}发布于 2015-09-22 12:20:48
尝尝这个,
你应该像这样创建广播接收器,
public class CallReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
// Phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Ringing state
// This code will execute when the phone has an incoming call
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is answered or disconnected
}
}
}还在manifest.xml文件中添加以下内容。
<receiver android:name=".CallReciever" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>发布于 2015-09-22 12:06:20
试试下面的代码:
private class PhoneStateChangeListener extends PhoneStateListener {
boolean isAnswered = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
isAnswered = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
isAnswered=true;
break;
case TelephonyManager.CALL_STATE_IDLE:
if(isAnswered == false)
//Call hangup without answered
break;
}
}
}https://stackoverflow.com/questions/32716227
复制相似问题