【问题标题】:sending broadcast intent to the appWidget provider from the service从服务向 appWidget 提供者发送广播意图
【发布时间】:2012-08-25 09:45:57
【问题描述】:

我想从服务更新小部件。所以我需要发送一个广播,它应该在 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();
    }

【问题讨论】:

  • 您是否在 manifest.xml 中注册了意图?

标签: android android-widget


【解决方案1】:

我想从服务更新小部件。

太棒了!

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

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

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

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

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

【讨论】:

  • 现在我遇到了问题。我正在为活动的共享偏好存储一些价值。如果我从服务中检索该值,则它不是更新的值。即使我在共享首选项 xml 中检查该值,我也会在那里看到更新的值。
  • @Paru:由于这与您的原始问题无关,请通过“”按钮提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 2011-11-08
  • 2023-03-10
  • 2019-07-24
  • 1970-01-01
相关资源
最近更新 更多