本文介绍了Android版的onReceive不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按照这个例子:
What是AppWidgetProvider之间共享数据和RemoteViewsService.RemoteViewsFactory
因此,我有一个 RemoteViewsFactory
有这样的:
As such, I have a RemoteViewsFactory
that has this:
@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
,但对我自己的意图广播,它永远不会触发的onReceive
(断点永远不会连打)。有谁看到我在做什么错了?
I get the debugger log for ACTION_APPWIDGET_UPDATE
, but for my own Intent broadcast, it never fires onReceive
(breakpoint is never even hit). Does anyone see what I'm doing wrong?
推荐答案
有关你自己的意图广播
工作,你必须注册
你的广播接收器
编程。因此,注册您的接收器
For your own Intent broadcast
to work, you have to register
your BroadcastReceiver
programmatically. So register your receiver as
LocalBroadcastManager.getInstance(this).registerReceiver(YourWidgetReceiver, new IntentFilter("your_intent_action"));
然后发送广播为
Intent intent = new Intent("your_intent_action");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent)
希望这有助于。
这篇关于Android版的onReceive不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!