【问题标题】:How to get widgetID and ComponentName of my application in service如何在服务中获取我的应用程序的 widgetID 和 ComponentName
【发布时间】:2013-07-19 10:40:17
【问题描述】:

我正在像这样从服务更新小部件(服务和小部件类在两个不同的包中)

AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
ComponentName widgetComponent = new ComponentName(this,MyWidgetProvider.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widgetComponent);
Intent update = new Intent();
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
 update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
sendBroadcast(update);

它更新了我的小部件,但问题是它也更新了设备的其他小部件,因此我的小部件在所有小部件都更新后更新。

所以现在我只想更新我的小部件,我该怎么做

【问题讨论】:

    标签: android android-widget android-service android-broadcast appwidgetprovider


    【解决方案1】:

    根据Android文档getAppWidgetIds返回一个已经绑定到特定WidgetProvider的id列表,这是你所有的widget实例id而不是其他WidgetProvider下的widget的id。

    【讨论】:

    • 那么解决办法是什么,我卡住了
    • 当您调用getAppWidgetIds 时,只会返回与您的小部件对应的ID,而不会返回其他WidgetProvider 的小部件。所以更新小部件的意图是在您的小部件实例上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多