从服务向appWidget提供者发送广播意图

时间:2012-08-25 09:45:58

标签: android android-widget

我想从服务更新小部件。所以我需要发送一个广播,它应该在AppWidgetProvider扩展的类中接收。我正在使用下面显示的代码。我无法在类的onReceive()中收到它。

if(getAction.equals("update.action")) {
        System.out.println("My Action");
        /*RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.mylayout);
        mAppWidgetManager.updateAppWidget(ids, views);*/
        Intent intent = new Intent("update.action", null);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,receivedIntent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0));
        sendBroadcast(intent);
        //updateAppWidget();
    }

1 个答案:

答案 0 :(得分:2)

  

我想从服务中更新小部件。

大!

  

所以我需要发送一个广播,它应该在AppWidgetProvider扩展的类中接收。

不,你没有。只需让您的服务使用AppWidgetManager更新应用小部件。

  

我使用的代码如下所示。我无法在班级的onReceive()中收到它。

正如Merlin所说,您可能没有调整清单中的<receiver>元素来反映这个新的操作字符串。

另外,请使用更有可能是唯一的操作字符串,例如将应用的包名称放在update.action之前。