shortcut 快捷方式创建及删除
AndroidShortCutUtils.java
/** * 添加到Shortcut选项中(默认桌面上长按调出) * @param activity * @param pakageName * @param className * @param shortcutName * @param icon * @param duplicate * * 同时需要在manifest中为activity提供一个包含 * action="android.intent.action.CREATE_SHORTCUT"的intent-filter */public static void addShortcutToOptions(Activity activity, String pakageName, String className, String shortcutName, Drawable icon, boolean duplicate){Intent shortcut = new Intent();String label = shortcutName;BitmapDrawable iconBitmapDrawabel = (BitmapDrawable)icon;PackageManager packageManager = activity.getPackageManager();try {ApplicationInfo appInfo = packageManager.getApplicationInfo(pakageName, PackageManager.GET_META_DATA|PackageManager.GET_UNINSTALLED_PACKAGES);if(label==null){label = packageManager.getApplicationLabel(appInfo).toString();}if(iconBitmapDrawabel==null){iconBitmapDrawabel = (BitmapDrawable) packageManager.getApplicationIcon(appInfo);}} catch (NameNotFoundException e) {e.printStackTrace();Toast.makeText(activity, e.toString(), Toast.LENGTH_SHORT);return;}shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmapDrawabel.getBitmap());ComponentName comp = new ComponentName(pakageName, className);shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));activity.setResult(Activity.RESULT_OK, shortcut);}static final String ACTION_INSTALL = "com.android.launcher.action.INSTALL_SHORTCUT";static final String ACTION_UNINSTALL = "com.android.launcher.action.UNINSTALL_SHORTCUT";/** * 添加快捷方式到桌面 * @param context * @param pakageName * @param className * @param shortcutName 可手动指定快捷方式的名称,删除时也要一致。null则使用默认名称 * @param icon 手动指定快捷方式的图标,null则使用默认图标 * @param duplicate * * 同时需要在manifest中设置以下权限: * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> */public static void addShortcutToDesktop(Context context, String pakageName, String className, String shortcutName, Drawable icon, boolean duplicate){Intent shortcut = new Intent(ACTION_INSTALL);String label = shortcutName;BitmapDrawable iconBitmapDrawabel = (BitmapDrawable)icon;PackageManager packageManager = context.getPackageManager();try {ApplicationInfo appInfo = packageManager.getApplicationInfo(pakageName, PackageManager.GET_META_DATA|PackageManager.GET_ACTIVITIES);if(label==null){label = packageManager.getApplicationLabel(appInfo).toString();}if(iconBitmapDrawabel==null){iconBitmapDrawabel = (BitmapDrawable) packageManager.getApplicationIcon(appInfo);}} catch (NameNotFoundException e) {e.printStackTrace();Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT);return;}shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmapDrawabel.getBitmap());shortcut.putExtra("duplicate", duplicate); ComponentName comp = new ComponentName(pakageName, className);shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(comp));context.sendBroadcast(shortcut);}/** * 删除桌面快捷方式 * @param context * @param pakageName * @param className * @param shortcutName 如果当初制定的快捷方式名称并非应用名,请手动指定,否则无法删除。null则使用默认名称 * * 同时需要在manifest中设置以下权限: * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> */public static void delShortcutFromDesktop(Context context, String pakageName, String className, String shortcutName){Intent shortcut = new Intent(ACTION_UNINSTALL);String label = shortcutName;PackageManager packageManager = context.getPackageManager();try {ApplicationInfo appInfo = packageManager.getApplicationInfo(pakageName, PackageManager.GET_META_DATA|PackageManager.GET_UNINSTALLED_PACKAGES);if(label==null){label = packageManager.getApplicationLabel(appInfo).toString();}} catch (NameNotFoundException e) {e.printStackTrace();Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT);return;}shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);ComponentName comp = new ComponentName(pakageName, className);shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));context.sendBroadcast(shortcut);}
?PS:关于删除
网上有人说shortcut的删除需要root。其实是不用的。
只是在删除的时候有点和创建不同的地方,看代码:
?
AndroidShortCutUtils.addShortcutToDesktop(this, this.getPackageName(), ".MainActivity", null, null, false);//删除的时候className需要包括有package的信息。AndroidShortCutUtils.delShortcutFromDesktop(this, this.getPackageName(), "com.knowhow.android.client.MainActivity", null);?
?