我是从我的旧代码中复制的,所以它可能需要一些润色。它不是开箱即用的,但是它包含了所有重要的步骤来告诉你如何去做。
-
首先,您需要显示一个选择器,让用户选择小部件:
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();