我正在基于股票启动器制作我的第一个 android 启动器。我正在尝试将小部件添加到我的主要线性布局中。但这不起作用。它没有比 onActivityResult() 方法更进一步。请帮助我,并提前非常感谢!我的代码:
private static final int REQUEST_CREATE_SHORTCUT = 1;
private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
private static final int REQUEST_CREATE_APPWIDGET = 5;
private static final int REQUEST_PICK_APPLICATION = 6;
private static final int REQUEST_PICK_SHORTCUT = 7;
private static final int REQUEST_PICK_LIVE_FOLDER = 8;
private static final int REQUEST_PICK_APPWIDGET = 9;
private static final int REQUEST_PICK_ANYCUT=10;
private static final int REQUEST_SHOW_APP_LIST = 11;
private static final int REQUEST_EDIT_SHIRTCUT = 12;
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, R.id.linearLayout);
//Add widgets
void selectWidget() {
int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
addEmptyData(pickIntent);
Log.d("addWidgetProcedure", "Starting activity to pick widget");
startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
}
void addEmptyData(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);
Log.d("addWidgetProcedure", "AddEmptyData");
};
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
Log.d("addWidgetProcedure", "inside onActivityResult");
if (resultCode == RESULT_OK ) {
Log.d("addWidgetProcedure", "RESULT_OK");
if (requestCode == REQUEST_PICK_APPWIDGET) {
Log.d("addWidgetProcedure", "calling configureWidget(data)");
configureWidget(data);
}
else if (requestCode == REQUEST_CREATE_APPWIDGET) {
Log.d("addWidgetProcedure", "calling createWidget(data)");
createWidget(data);
}
}
else if (resultCode == RESULT_CANCELED && data != null) {
Log.d("addWidgetProcedure", "RESULT_CANCELED");
int appWidgetId =
data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
if (appWidgetId != -1) {
mAppWidgetHost.deleteAppWidgetId(appWidgetId);
}
}
}
private void configureWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo =
mAppWidgetManager.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, REQUEST_CREATE_APPWIDGET);
} else {
createWidget(data);
}
}
public void createWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo =
mAppWidgetManager.getAppWidgetInfo(appWidgetId);
AppWidgetHostView hostView =
mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
linearLayout.addView(hostView);
Toast.makeText(getApplicationContext(), "Adding widget", 15).show();
}
当我按菜单-> 添加小部件时,它会调用 selectWidget() 方法。
当我按下菜单->添加小部件时,所有的 logcat 输出我都选择了一个。
07-11 14:38:27.178: W/KeyCharacterMap(1751): Bad keycharmap - filesize=32
07-11 14:38:27.178: W/KeyCharacterMap(1751): Error loading keycharmap file '/system/usr/keychars/bravo-keypad.kcm.bin'. hw.keyboards.0.devname='bravo-keypad'
07-11 14:38:27.178: W/KeyCharacterMap(1751): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-11 14:38:28.348: D/addWidgetProcedure(1751): AddEmptyData
07-11 14:38:28.348: D/addWidgetProcedure(1751): Starting activity to pick widget
07-11 14:38:28.428: D/addWidgetProcedure(1751): inside onActivityResult