我正在使用以下代码创建主页快捷方式:我想获得启动的应用程序的相同打开实例(如果存在)而不是新实例。
public void createShortcut() {
if (app.prefs.getBoolean("prefs_ShortcutCreated", false) == false) {
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test");
addIntent.putExtra("duplicate", false);
File image = new File(app.DEFAULT_APP_FOLDER_MAIN, "icon.png");
AssetManager assets = app.getApplicationContext().getResources().getAssets();
try {
copy(assets.open("icon.png"), image);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap theBitmap = BitmapFactory.decodeFile(app.DEFAULT_APP_FOLDER_MAIN + "icon.png");
Bitmap scaledBitmap = Bitmap.createScaledBitmap(theBitmap, 128, 128, true);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(addIntent);
theBitmap.recycle();
scaledBitmap.recycle();
Editor editor = app.prefs.edit();
editor.putBoolean("prefs_ShortcutCreated", true);
editor.commit();
}
}