【问题标题】:Implicit Broadcast Reciever isn't calling隐式广播接收器未调用
【发布时间】:2018-06-02 21:07:29
【问题描述】:

我在网上搜索了很多时间,但我不明白为什么我的自定义广播 不工作。

<receiver
        android:name=".myservice.MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            <action android:name="android.intent.action.BATTERY_CHANGED"/>
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.SCREEN_OFF" />
        </intent-filter>
</receiver>

当我重新连接并断开充电器时,它没有收到。

我这样做是为了让事情变得简单

public class MyReceiver extends BroadcastReceiver
{
   @Override
   public void onReceive(Context context, Intent intent)
   {
       Toast.makeText(context,"Battery", Toast.LENGTH_SHORT).show();
       Log.i("Recive", "Yes");
   }
}

【问题讨论】:

  • 你的 logcat 不打印 Log.i("Recive", "Yes");? (过滤 logcat 以显示信息消息)
  • 您使用的是什么设备和安卓版本?
  • 它没有打印,我使用的是 oneplus 3t android 8
  • 您使用哪个 targetSdkVersion?我的答案适用于 targetSdkVersion 25 或更低版本的 Android 8 (Nexus 5X)。没有使用 targetSdkVersion 26 或更高版本对其进行测试。
  • 我的手机是sdk 26

标签: android broadcastreceiver android-8.0-oreo


【解决方案1】:

来自docs

ACTION_BATTERY_CHANGED 广播动作:这是一个粘性广播,包含充电状态、电量和其他有关电池的信息。有关 Intent 内容的文档,请参阅 BatteryManager。

您无法通过清单中声明的​​组件接收此信息,只能通过 Context.registerReceiver() 显式注册它。请参阅 ACTION_BATTERY_LOW、ACTION_BATTERY_OKAY、ACTION_POWER_CONNECTED 和 ACTION_POWER_DISCONNECTED,了解可通过清单接收器发送和接收的不同电池相关广播

因此,您不能在Manifest 中使用此贴花BroadcastReceiver,只能从您的上下文中显式注册。

另外,您的电源连接BroadcastReceiver 似乎是正确的。尝试将其分离为另一个BroadcastReceiver,可能动作ACTION_BATTERY_CHANGED 正在干扰其他动作。

这是我声明的BroadcastReceiver,我使用它,它在我的应用程序中工作。

<receiver android:name=".PowerConnectionBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
        </intent-filter>
</receiver>

PowerConnectionBroadcastReceiver

public class PowerConnectionBroadcastReceiver extends BroadcastReceiver {
  private static final String TAG = "PowerRcvr";
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
      Log.d(TAG, "Device is charging");
    } else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
      Log.d(TAG, "Device is NOT charging");
    } else {
      Log.d(TAG, "Unable to check if device is charging or not");
    }
  }
}

注意:此代码适用于具有 targetSdkVersion 25 或更低版本的 Android 8。

在 targetSdkVersion 26 或更高版本中,由于背景限制,BroadcastReceivers 的大部分内容无法通过 Manifest 工作。这是documentation(感谢Pawel)关于这个。所以你的IntentFilters 不会工作。为了让它继续工作,您可以将 targetSdkVersion 下载到 25 或更低版本。

【讨论】:

  • 我认为 OP 正试图在 android O 上使用它,除了一些 exceptions 之外,隐式广播根本不起作用。
  • 所以我不能广播电缆连接?还有另一种方法吗?因为我使用的是android O (26)。
  • 您不能通过 Manifest 广播 BATTERY_CHANGED(但您可以通过上下文以编程方式手动进行)。电缆连接可以在 Manifest 中声明,我的代码在 Android 8 中运行良好,但使用 targetSdkVersion 25 或更低版本编译。
  • 你能解释一下为什么它不适用于 targetSdkVersion 26+ 吗?
  • 这是有限的,因为使用 targetSdkVersion 26+ 系统优化不允许接收此类广播。使用 targetSdkVersion 25 时,您的应用不会触发此优化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多