我正在开发一个响应来电的 Android 应用程序。我已经设置了一个广播接收器,在 list 文件中设置了权限和 Intent 过滤器,而且很多时候,它都有效。我可以杀死所有应用程序,重新启动手机,它仍然有效..
但是有时候没有反应,出现一次后,重启后也没有反应。
我已经为此苦苦挣扎了大约一个月,它让我发疯。我真的需要我的应用程序在每次收到来电时做出响应。
我已经尝试删除所有代码,并在收到或结束调用时仅显示 Toast 消息。即使应用程序除此之外什么都不做,行为仍然相同。
我无法在模拟器上复制它,我正在使用我的HUAWEI ASCEND Y550 进行设备测试。
非常感谢任何帮助,
大卫
list :
<?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)
{
}
}
}
}
最佳答案
我想我可能已经解决了这个问题 - 虽然我是开发甚至使用 Android 手机的新手...
在我的华为手机中,我在“ protected 应用程序”设置中有一个选项 - 那些不会被杀死的应用程序。
我看过几个 friend 的 Android 手机,但一直找不到相同的设置 - 所以我可以说这是华为的一项特定功能,可以弥补电池电量不足的问题。
谁能确认这是华为特有的问题?
大卫
关于android - BroadcastReceiver 工作了一段时间,然后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28377792/