gpt4 book ai didi

Android onReceive 未调用

转载 作者:行者123 更新时间:2023-11-30 01:34:33 26 4
gpt4 key购买 nike

我正在尝试遵循这个例子: What is the correct way for sharing data among AppWidgetProvider and RemoteViewsService.RemoteViewsFactory

因此,我有一个 RemoteViewsFactory,其中包含:

@Override
public void onDataSetChanged() {
// Subsequent calls to get the data.
newsGetter.updateListFeed(null, new NewsGetter.OnUpdateListFeedFinishedListener() {
@Override
public void onUpdateListFeedFinished(VolleyError error) {
//async return here from volley

Intent widgetUpdateIntent = new Intent(NewsWidgetBase.FEED_UPDATED);
widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
LocalBroadcastManager.getInstance(context).sendBroadcast(widgetUpdateIntent);
}
});

Log.e(TAG, "******************************** onDataSetChanged PROVIDER");
}

然后我有一个 AppWidgetProvider 有这个:

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

final String action = intent.getAction();

if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
Log.e("WidgetBase", "******************************** onReceive ACTION_APPWIDGET_UPDATE");
} else if (FEED_UPDATED.equals(action)) {
Log.e("WidgetBase", "******************************** onReceive FEED_UPDATED");

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
updateAppWidget(context, appWidgetManager, appWidgetId);
}

super.onReceive(context, intent);
}

在 list 中:

    <receiver android:name=".widgets.a">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.myapp.FEED_UPDATED" />
</intent-filter>

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

我得到了 ACTION_APPWIDGET_UPDATE 的调试器日志,但是对于我自己的 Intent 广播,它从不触发 onReceive(甚至从未命中断点)。有人看到我做错了什么吗?

最佳答案

要使您自己的Intent 广播 正常工作,您必须以编程方式注册您的BroadcastReceiver。因此,将您的接收器注册为

LocalBroadcastManager.getInstance(this).registerReceiver(YourWidgetReceiver, new IntentFilter("your_intent_action"));

然后将您的广播发送为

Intent intent = new Intent("your_intent_action");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent)

;

希望这对您有所帮助。

关于Android onReceive 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263508/

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