在您的应用程序中,使用以下代码,我可以获得安装在设备上的所有小部件的提供程序列表:
AppWidgetManager manager = AppWidgetManager.getInstance(this);
List <ComponentName> cn = new ArrayList<>();
List<AppWidgetProviderInfo> infoList = manager.getInstalledProviders();
for (AppWidgetProviderInfo info : infoList) {
cn.add(info.provider);
}
如果我要启动这些小部件之一,有没有办法做到这一点? (我想我可能需要appWidgetId,但我不知道如何检索它。)
答案 0 :(得分:1)
我从我的旧代码中复制了此代码,因此可能需要一些改进。它不会立即可用,但是包含了所有重要步骤来向您展示如何做到这一点。
private void createWidget() {
try {
awhId = // generate your unique ID
awh = new AppWidgetHost(ctx, awhId);
int appWidgetId = awh.allocateAppWidgetId();
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
pickIntent = widgetAddEmptyData(pickIntent);
startActivityForResult(pickIntent, ActivityMain.REQUEST_CODE_WIDGET_ADD);
} catch (Exception e) {
widgetError(e.getMessage(), e);
}
}
public Intent widgetAddEmptyData(Intent pickIntent) {
ArrayList<AppWidgetProviderInfo> customInfo = new ArrayList<AppWidgetProviderInfo>();
pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
return pickIntent;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
boolean processed = false;
if (resultCode == Activity.RESULT_OK) {
if (requestCode == ActivityMain.REQUEST_CODE_WIDGET_ADD) {
widgetConfigure(data);
processed = true;
}
if (requestCode == ActivityMain.REQUEST_CODE_WIDGET_CONFIGURED) {
widgetSave(data);
processed = true;
}
} else if (resultCode == Activity.RESULT_CANCELED && data != null) {
int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
if (appWidgetId != -1) {
try {
if (awh == null) {
awh = new AppWidgetHost(ctx, awhId);
}
awh.deleteAppWidgetId(appWidgetId);
} catch (Exception e) {}
}
}
if (!processed) {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void widgetConfigure(Intent data) {
try {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = wm.getAppWidgetInfo(appWidgetId);
if (appWidgetInfo.configure != null) {
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setComponent(appWidgetInfo.configure);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
startActivityForResult(intent, ActivityMain.REQUEST_CODE_WIDGET_CONFIGURED);
} else {
widgetSave(data);
}
} catch (Exception e) {
widgetError(e.getMessage(), e);
}
}
private void widgetSave(final Intent data) {
try {
Bundle extras = data.getExtras();
final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
final AppWidgetProviderInfo appWidgetInfo = wm.getAppWidgetInfo(appWidgetId);
// store information about created widget
store(MyApps.WIDGET_HOST_ID, awhId);
store(MyApps.WIDGET_ID, appWidgetId);
store(MyApps.WIDGET_ICON, appWidgetInfo.icon);
store(MyApps.WIDGET_PACKAGE, appWidgetInfo.provider.getPackageName());
store(MyApps.WIDGET_CLASS, appWidgetInfo.provider.getClassName());
}
AppWidgetManager wm = AppWidgetManager.getInstance(getContext());
awh = new FaAppWidgetHost(getContext(), myAppItem.getWidgetHostId());
AppWidgetProviderInfo appWidgetInfo = wm.getAppWidgetInfo(myAppItem.getWidgetId());
AppWidgetHostView hostView = awh.createView(getContext(), myAppItem.getWidgetId(), appWidgetInfo);
hostView.setAppWidget(myAppItem.getWidgetId(), appWidgetInfo);
awh.startListening();