【问题标题】:BroadcastReceiver works for a while, then stopsBroadcastReceiver 工作一段时间,然后停止
【发布时间】: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


【解决方案1】:

我想我可能已经解决了这个问题 - 虽然我是开发甚至使用 Android 手机的新手......

在我的华为手机中,我的设置中有一个“受保护的应用程序”选项 - 那些不会被杀死的应用程序。

我查看了一些朋友的安卓手机,但无法找到相同的设置 - 所以我可以说这是华为特有的功能,可以弥补电池电量不足的问题。

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

大卫

【讨论】:

  • 是的,有同样的问题。
【解决方案2】:

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

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

【讨论】:

  • 您好,感谢您的建议...试过了,但仍然没有成功。仍然得到相同的行为...... :(
猜你喜欢
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-15
  • 2013-11-19
  • 1970-01-01
  • 2010-11-30
相关资源
最近更新 更多