gpt4 book ai didi

android - 不适用于 Oreo 8.1 的小部件案例 - 收到消息 : W/BroadcastQueue: Background execution not allowed: receiving Intent

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:03 25 4
gpt4 key购买 nike

我的小部件应用程序在除 8 Oreo 之外的所有 Android 版本上运行良好。我收到一条 W/BroadcastQueue: Background execution not allowed: receiving Intent 消息。

CommonsWare 有一篇有趣的博客,但我不完全理解为什么它适用于我的案例。 https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html

我的案例看起来很简单:我有一个带按钮的小部件,我想在单击时更改文本的按钮。

解决此问题的正确方法是什么?

测试控件.java

public class TestWidget extends AppWidgetProvider {
private static RemoteViews views;
private static boolean buttonClicked = false;
public static final String ACTION_AUTO_UPDATE = "AUTO_UPDATE";

@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);

if(intent.getAction().equals(ACTION_AUTO_UPDATE))
{
Log.i("TESTWID", "get onReceive");
}
}

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
views.setOnClickPendingIntent(R.id.wid_btn_tst, setButton(context));

appWidgetManager.updateAppWidget(appWidgetId, views);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.i("TESTWID", "onupdate ");

for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}

public static PendingIntent setButton(Context context) {
Intent intent = new Intent();
intent.setAction("TEST");
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, TestWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}

}

TestWidgetReceiver.java

public class TestWidgetReceiver extends BroadcastReceiver{
private static boolean isButtonON = false;

@Override
public void onReceive(Context context, Intent intent) {
Log.i("TESTWID", "onReceive "+intent.getAction());

if(intent.getAction().equals("TEST")){
updateWidgetButton(context, 2);
}
}

private void updateWidgetButton(Context context, int index) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.test_widget);
if(index == 2) {
if(isButtonON) {
remoteViews.setTextViewText(R.id.wid_btn_tst, "Test Off");
isButtonON = false;
}
else{
remoteViews.setTextViewText(R.id.wid_btn_tst, "Test On");
isButtonON = true;
}
}

TestWidget.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
}

}

list .xml:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Test"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".TestWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="AUTO_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/test_widget_info" />
</receiver>

<receiver
android:name=".TestWidgetReceiver"
android:label="widgetBroadcastReceiver" >
<intent-filter>
<action android:name="TEST" />
</intent-filter>

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

</application>

最佳答案

这很微妙,但这是因为隐式广播被用来触发您的TestWidgetReceiver。它是隐式的,因为它仅指定 Intent 的操作部分。通过在构造函数中指定接收器类,使广播 Intent 显式:

public static PendingIntent setButton(Context context) {
Intent intent = new Intent(context, TestWidgetReceiver.class);
intent.setAction("TEST");
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

关于android - 不适用于 Oreo 8.1 的小部件案例 - 收到消息 : W/BroadcastQueue: Background execution not allowed: receiving Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50894567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com