我正在开发一个响应传入呼叫的Android应用程序。我已经建立了一个广播接收器,在清单文件中设置权限和意图过滤器,以及很多时间,它都可以工作。我可以杀死所有的应用程序,重新启动手机,它仍然可以工作。BroadcastReceiver工作一段时间,然后停止
但是,有时它不反应,并且在此之后发生一次,重新启动后它也不响应。
我一直在为此奋斗了大约一个月,这是驱动我的香蕉。每次收到来电时,我都非常需要我的应用程序进行响应。
我试着修剪掉所有的代码,并在收到或结束呼叫时显示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)
{
}
}
}
}
它是否可以在其他设备上重现? – AADProgramming 2015-02-07 02:43:53
嗨,不幸的是,我没有任何其他设备来测试它... 希望它在我发布前正常工作... – 2015-02-07 02:47:07
我不认为你的代码有问题。我在两个应用程序中做了完全相同的事情,并且它几乎可以一直工作。有一件事你应该注意的是,TelephonyManager.EXTRA_STATE可能不会总是返回一个值,并且某些设备可能需要intent过滤器中的“android:priority”,正如本文中讨论的:http:// stackoverflow。 com/questions/5817178 /广播 - 接收器接收电话优先 – 2015-02-07 02:53:43