Android onReceive没有调用

时间:2016-02-08 06:22:30

标签: android android-widget

我试图效仿这个例子: 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(断点永远不会被击中)。有谁看到我做错了什么?

1 个答案:

答案 0 :(得分:1)

要让自己的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)

然后将您的广播发送为

1.5 dp  lax4    {107:107,111:111,508:508,509:509,654:654,662:662,685:685}   [107,662,685,508,111,654,509]   drudgereport.com    appnexus    http://drudgereport.com/    c       1454822402  2016    2   7   5
1.5 dp  lax4    {10175:10175,10325:10325,310:310,313:313,316:316,365:365,380:380,509:509}   [310,365,509,316,313,380,10175,10325]   www.worldlifestyle.com  appnexus    http://www.worldlifestyle.com/beauty-style/most-iconic-wedding-dresses-in-history-14d/16    c       1454824483  2016    2   7   5

希望这有帮助。