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

2023-12-01

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

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

我收到以下错误:

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);
    }

project link以供参考。遵循了以前的帖子,但没有成功。

Oreo - 小部件服务和广播接收器:不允许启动服务 Intent


问题出在您正在制作的隐式广播中发送Widget刷新。 您可以通过在意图中定义组件名称来突破禁令。

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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

小部件问题:BroadcastQueue:不允许后台执行:接收 Intent 的相关文章

随机推荐