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

时间:2018-12-02 07:23:38

标签: android android-intent android-widget android-broadcast

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

  • 它可以在旧的targetdk设备上完美运行。

我遇到以下错误:

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>

还有我的尊敬的班级代码(

 @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的位置。

世界时钟活动

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

项目link供参考。关注了以前的帖子,但是没有用。

Oreo - Widget services and broadcast receiver: Not allowed to start service Intent

2 个答案:

答案 0 :(得分:2)

问题出在您在 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);
}

答案 1 :(得分:0)

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