我需要找到任何AppWidget id来测试一些功能。执行此操作的代码:
List<AppWidgetProviderInfo> infos = mAppWidgetManager.getInstalledProviders();
for (int i = 0; i < infos.size(); i++) {
int ids[] = mAppWidgetManager.getAppWidgetIds(infos.get(i).provider);
if (ids.length != 0) {
Log.d("TAG", "Found non-zero-length provider!");
id = ids[0];
info = infos.get(i);
break;
}
}
但是ids []长度始终为0!为什么以及如何解决它?
更新:我已将此行添加到循环的开头:
Log.d("TAG", provider.getPackageName() + "; " + provider.getClassName());
它打印出来:
D/TAG ( 5686): Providers count: 12
D/TAG ( 5686): com.andrew.apollo; com.andrew.apollo.app.widgets.AppWidget11
D/TAG ( 5686): com.andrew.apollo; com.andrew.apollo.app.widgets.AppWidget41
D/TAG ( 5686): com.andrew.apollo; com.andrew.apollo.app.widgets.AppWidget42
D/TAG ( 5686): com.android.browser; com.android.browser.widget.BookmarkThumbnailWidgetProvider
D/TAG ( 5686): com.android.calendar; com.android.calendar.widget.CalendarAppWidgetProvider
D/TAG ( 5686): com.android.contacts; com.android.contacts.socialwidget.SocialWidgetProvider
D/TAG ( 5686): com.android.deskclock; com.android.alarmclock.AnalogAppWidgetProvider
D/TAG ( 5686): com.android.email; com.android.email.provider.WidgetProvider
D/TAG ( 5686): com.android.gallery3d; com.android.gallery3d.gadget.PhotoAppWidgetProvider
D/TAG ( 5686): com.android.quicksearchbox; com.android.quicksearchbox.SearchWidgetProvider
D/TAG ( 5686): com.android.settings; com.android.settings.widget.SettingsAppWidgetProvider
D/TAG ( 5686): com.android.vending; com.android.vending.MarketWidgetProvider
所以,它似乎没问题,但我仍然无法获得这些小部件的ID。
答案 0 :(得分:5)
确保您使用ComponentName
AppWidgetManager
与清单中的小部件提供程序匹配。如果没有,你将得到一个空列表(ids.length = 0,循环不会执行)。
答案 1 :(得分:2)
我为您的小部件设置了不同的布局和大小,因此检索所有ID的不同小部件提供商结果并非那么简单。要获取所有窗口小部件提供程序的所有窗口小部件ID,您可以执行以下操作:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int Widged_IDS_A[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderA.class));
int Widged_IDS_B[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderB.class));
int Widged_IDS_C[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderC.class));
int Widged_IDS_D[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderD.class));
int Widged_IDS_E[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProviderE.class));
int allWidgetIds[] = new int[Widged_IDS_A.length + Widged_IDS_B.length + Widged_IDS_C.length + Widged_IDS_D.length + Widged_IDS_E.length];
System.arraycopy(Widged_IDS_A, 0, allWidgetIds, 0, Widged_IDS_A.length);
System.arraycopy(Widged_IDS_B, 0, allWidgetIds, Widged_IDS_A.length, Widged_IDS_B.length);
System.arraycopy(Widged_IDS_C, 0, allWidgetIds, Widged_IDS_A.length + Widged_IDS_B.length, Widged_IDS_C.length);
System.arraycopy(Widged_IDS_D, 0, allWidgetIds, Widged_IDS_A.length + Widged_IDS_B.length + Widged_IDS_C.length, Widged_IDS_D.length);
System.arraycopy(Widged_IDS_E, 0, allWidgetIds, Widged_IDS_A.length + Widged_IDS_B.length + Widged_IDS_C.length + Widged_IDS_D.length, Widged_IDS_E.length);
Log.v(Classname, "allWidgetIds.length: " + allWidgetIds.length);
allWidgetIds = merge(Widged_IDS_A, Widged_IDS_B, Widged_IDS_C, Widged_IDS_D, Widged_IDS_E);
for (int x : allWidgetIds) {
Log.v(Classname, x + " allWidgetIds onReceive()");
}