小部件问题:BroadcastQueue:不允许后台执行:接收 Intent

Widget issue: BroadcastQueue: Background execution not allowed: receiving Intent

我的应用程序小部件在升级到 targetSDK 到 28 后停止工作。

我收到以下错误:

W/BroadcastQueue: Background execution not allowed: receiving Intent { act=ch.corten.aha.worldclock.WIDGET_DATA_CHANGED flg=0x10 } to ch.corten.aha.worldclock/.WorldClockWidgetProvider
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=ch.corten.aha.worldclock.WIDGET_DATA_CHANGED flg=0x10 } to ch.corten.aha.worldclock/.WeatherWidgetProvider

androidmanifest.xml 文件内容如下-

       <!-- clock widget -->
    <receiver
        android:name=".WorldClockWidgetProvider"
        android:exported="false"
        android:label="@string/clock_widget_name" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
        </intent-filter>
        <intent-filter>
            <action android:name="ch.corten.aha.worldclock.WIDGET_DATA_CHANGED" />
        </intent-filter>
        <intent-filter>
            <action android:name="ch.corten.aha.worldclock.CLOCK_TICK" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/world_clock_appwidget_info" />
    </receiver>
<receiver
        android:name=".WeatherWidgetProvider"
        android:enabled="@bool/enable_weather_widget"
        android:exported="false"
        android:label="@string/weather_widget_name" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
        </intent-filter>
        <intent-filter>
            <action android:name="ch.corten.aha.worldclock.WIDGET_DATA_CHANGED" />
        </intent-filter>
        <intent-filter>
            <action android:name="ch.corten.aha.worldclock.CLOCK_TICK" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/weather_appwidget_info" />
    </receiver>

还有我的回复 class 代码(

 @Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (WIDGET_DATA_CHANGED_ACTION.equals(intent.getAction())
            || CLOCK_TICK_ACTION.equals(intent.getAction())) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (pm.isScreenOn()) {
            onClockTick(context);
        }
    }
}

其中时钟小部件提供程序正在扩展 AppWidgetProvider。

世界时钟activity

  private static void sendWidgetRefresh(Context context) {
        // send update broadcast to widget
        Intent broadcast = new Intent(ClockWidgetProvider.WIDGET_DATA_CHANGED_ACTION);
        context.sendBroadcast(broadcast);
    }

项目link供参考。按照以前的帖子,但没有奏效。

问题出在您在 sendWidgetRefresh 中进行的隐式广播中。 你可以通过在你的意图中定义一个组件名称来突破禁令。

private static void sendWidgetRefresh(Context context) {
    // send update broadcast to widget
    Intent broadcast = new Intent(ClockWidgetProvider.WIDGET_DATA_CHANGED_ACTION);
    ComponentName componentName = new ComponentName(context, WorldClockWidgetProvider.class);
    broadcast.setComponent(componentName);
    context.sendBroadcast(broadcast);
}

这对我有用! 将您的隐式意图更改为显式意图,因为启动 Oreo 隐式意图不会 运行 在后台! https://developer.android.com/about/versions/oreo/background.html