我试图让我的应用程序识别时,有一个错误的电话。我已经创建了一个BroadcastReceiver来检测手机的状态何时发生变化。下面是我的onReceive()方法:
public void onReceive(Context context, Intent intent) {
boolean ringing = false, callReceived = false;
String callerPhoneNumber = "";
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
ringing = true;
Bundle bundle = intent.getExtras();
callerPhoneNumber= bundle.getString("incoming_number");
if (ringing) {
Toast.makeText(context, "Phone call from: " + callerPhoneNumber, Toast.LENGTH_LONG).show();
}
}
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
callReceived = true;
Toast.makeText(context, "Call received.", Toast.LENGTH_LONG).show();
}
if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
// Toast.makeText(context, "Phone is idle", Toast.LENGTH_LONG).show();
if (ringing) {
Toast.makeText(context, "ringing is true", Toast.LENGTH_LONG).show();
}
// if (ringing && !callReceived) {
// Toast.makeText(context, "It was A MISSED CALL from : " + callerPhoneNumber, Toast.LENGTH_LONG).show();
// }
}我遇到的问题是,应用程序检测电话铃声,将ringing设置为true,并显示适当的祝酒词。但是,当手机的状态是idle时,它不会输入if (ringing)语句来显示ringing is true toast消息。然而,如果我在上面的祝酒词中发表评论,它将正确地打印出Phone is idle。
ringing是否有可能被重置为false,介于电话铃响和手机再次空闲之间?
发布于 2014-04-24 13:55:53
您不能在广播接收器中存储任何“状态”信息。如果要比较旧状态/新状态,我认为可以使用服务。
https://stackoverflow.com/questions/23270982
复制相似问题