在活动AppWidgetManager.getAppWidgetIds返回一个空列表 [英] AppWidgetManager.getAppWidgetIds in activity returns an empty list
问题描述
我有我试图从活动更新appwidget。
I have an appwidget that I'm trying to update from an activity.
要做到这一点,我需要的appwidget ID。
To do that, I need the appwidget id.
我用 AppWidgetManager.getAppWidgetIds
,但它总是返回一个空列表。
I've used AppWidgetManager.getAppWidgetIds
but it always returns an empty list.
我还用 AppWidgetManager.getInstalledProviders
来确保我的组件名
是正确的,但我仍然得到一个空列表中。
I also used AppWidgetManager.getInstalledProviders
to make sure that my ComponentName
is correct, but still I get an empty list.
我已经看到了所有关于这个的其他问题,但我无法找到的东西,为我工作。
I've seen all the other questions about this, but I couldn't find something that worked for me.
有另一种方式来解决这个问题?或者另一种方式来更新窗口小部件?
Is there another way to solve this? or another way to update the widget?
我的code:
ComponentName name = new ComponentName(packageName, boardcastReceiverClass);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
int[] ids = appWidgetManager.getAppWidgetIds(name);
if (ids != null && ids.length > 0) {
getApplicationContext().sendBroadcast(getUpdateIntent(ids[0]));
}
感谢。
更新:我要指出,我AppWidgetProvider在库项目。根据我与 getInstalledProviders
的组件名,我用我的应用程序的包名和库的包名的类名。
UPDATE: I should mention that my AppWidgetProvider is in a library project. According to the ComponentName I get with getInstalledProviders
, I used the package name of my app and the class name with the package name of the library.
推荐答案
您可以访问你的widget不知道ID
You can access your widget without knowing id
appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(),Widget.class.getName()), views);
这允许您访问的小部件,而无需知道appWidgetID。然后,在活动:
This let you access the widget without having to know the 'appWidgetID'. Then in activity:
Intent intent = new Intent(this, Settings.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnActivate, pendingIntent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);
finish();
这篇关于在活动AppWidgetManager.getAppWidgetIds返回一个空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!