我需要找到任何 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。