【发布时间】:2015-02-07 02:11:10
【问题描述】:
我正在开发一个响应来电的安卓应用。我已经设置了一个广播接收器,在清单文件中设置了权限和意图过滤器,而且很多时候,它都可以工作。我可以杀死所有应用程序,重新启动手机,它仍然可以工作..
但是,有时它没有反应,并且在这种情况发生一次之后,它在重新启动后也没有反应。
我已经为此苦苦挣扎了大约一个月,这让我很生气。每次收到来电时,我真的需要我的应用程序做出响应。
我已尝试删除所有代码,并在收到或结束呼叫时仅显示 Toast 消息。即使应用程序只执行此操作,行为仍然相同。
我无法在模拟器上复制它,我正在使用我的 HUAWEI ASCEND Y550 进行设备测试。
非常感谢任何帮助,
大卫
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.app.receivertest" >
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
我的接收者:
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
if (context == null && intent == null)
return;
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String action = intent.getAction();
Toast.makeText(context, action + " " + state, Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
try {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
}
}
}
}
【问题讨论】:
-
是否也可以在其他设备上重现?
-
嗨,很遗憾,我没有任何其他设备可以测试它...希望在我发布之前让它正常工作...
-
我不认为你的代码有问题。我在我的两个应用程序中做了完全相同的事情,而且它几乎一直运行良好。您应该注意的一件事是
TelephonyManager.EXTRA_STATE可能并不总是返回值,并且某些设备可能需要在意图过滤器中使用android:priority,如本文所述:stackoverflow.com/questions/5817178/… -
干杯伙伴,我会调查的...谢谢你的帮助!
标签: android broadcastreceiver huawei-mobile-services