我正在尝试遵循以下示例:
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);
}
并在清单中:
<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 broadcast
工作,您必须以编程方式register
您的BroadcastReceiver
。因此,将您的接收者注册为
LocalBroadcastManager.getInstance(this).registerReceiver(YourWidgetReceiver, new IntentFilter("your_intent_action"));
然后将您的广播发送为
Intent intent = new Intent("your_intent_action");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent)
;
希望这可以帮助。