我正在制作我的第一个基于股票发射器的机器人启动器。我正在尝试将小部件添加到我的主要线性布局上。但这不起作用。它没有比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();
}
当我按下menu-&gt;添加小部件时,它会调用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
答案 0 :(得分:0)
尝试替换你的:
private static final int REQUEST_PICK_APPWIDGET = 9;
使用:
private static final int REQUEST_PICK_APPWIDGET = 0;