BroadcastReceiver 工作了一段时间,然后停止

BroadcastReceiver works for a while, then stops

我正在开发一个 android 应用来响应来电。我已经设置了一个广播接收器,在清单文件中设置了权限和意图过滤器,而且很多时候,它都有效。我可以杀死所有应用程序,重新启动 phone 它仍然有效..

但有时无反应,出现一次后,重启后也无反应。

我已经为此苦苦挣扎了大约一个月,它让我发疯。我真的需要我的应用程序在每次收到来电时做出响应。

我试过删除所有代码,只在接到电话或结束时显示 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)
        {
        }
    }
  }
}

你可以尝试设置优先级。像这样:

<intent-filter android:priority="1000">

我想我可能已经解决了这个问题 - 尽管我是开发甚至使用 android phones 的新手...

在我的华为 phone 中,我的设置中有一个选项 "Protected Apps" - 那些不会被杀死的选项。

我查看了几个朋友 android phone 并且无法找到相同的设置 - 所以我说这是华为的特定功能可以弥补电池不好。

谁能确认这是华为特有的问题?

大卫