创建与删除快捷方式
创建与删除快捷方式:
权限:<!-- 创建与删除快捷方式 --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
public class ShortCutUtil {
/**** 桌面快捷方式相关* * @author chenzheng_java* @since 2011/07/20*/
// 创建桌面快捷方式需要的intent字符串,此字符串为指定值,不能修改private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";// 删除桌面快捷方式需要的intent字符串,此字符串为指定值,不能修改private static final String ACTION_UNINSTALL_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";private static final String SHORTCUT_DUPLICATE = "duplicate";private static final String shortcut_actionString = "cn.com.chenzheng_java.shortcut";
/**** 创建一个快捷方式到桌面* * @param shortcutName* 快捷方式的名称* @param activity* 一个Activity对象* @param iconId* 当做快捷图标的图片的索引值*/public static void createShortcut(String shortcutName, Activity activity,int iconId) {
Intent shortcut_intent = new Intent();shortcut_intent.setAction(ACTION_INSTALL_SHORTCUT);// 发送此广播,则系统会为我们创建快捷方式到桌面shortcut_intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);// 设置快捷方式的名称
shortcut_intent.putExtra(SHORTCUT_DUPLICATE, false);// 是否可以重复创建
Intent intent = new Intent();// intent.setClass(activity, activity.getClass()); 如此也可以ComponentName componentName = new ComponentName(activity, activity.getClass());intent.setComponent(componentName);
intent.setAction(shortcut_actionString);shortcut_intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);// 设置当快捷方式被点击时触发哪个intent
// 快捷方式的图标Parcelable icon = ShortcutIconResource.fromContext(activity, iconId);shortcut_intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);activity.sendBroadcast(shortcut_intent);// 向系统发送广播,创建快捷方式
}
/**** 删除当前应用的快捷方式* * @param shortcutName* 快捷方式的名称* @param activity* 当前上下文*/public static void deleteShortcut(String shortcutName, Activity activity) {
Intent shortcut = new Intent(ACTION_UNINSTALL_SHORTCUT);// 显示的名字shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);// 这里的intent要和创建时的intent设置一致// Intent intent = new Intent(activity, activity.getClass());Intent intent = new Intent();ComponentName componentName = new ComponentName(activity, activity.getClass());intent.setComponent(componentName);intent.setAction(shortcut_actionString);shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// 发送广播以删除shortcutactivity.sendBroadcast(shortcut);// Toast.makeText(activity, "删除自身快捷方式成功", Toast.LENGTH_SHORT).show();
}
/**** 删除快捷方式* * @param shortcutName* 快捷方式名称* @param componentName* @param addActionString* @param context*/public static void deleteShortcut(String shortcutName,ComponentName componentName, String addActionString, Context context) {Intent shortIntent = new Intent();shortIntent.setAction(ACTION_UNINSTALL_SHORTCUT);shortIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
Intent intent = new Intent();intent.setComponent(componentName);intent.setAction(addActionString);shortIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
context.sendBroadcast(shortIntent);
}
/**** 删除某个快捷方式* @param intent* 关联快捷方式与应用程序之间的intent* @param shortcutName 快捷方式的名称* @param context 上下文*/public static void deleteShortcut(Intent intent, String shortcutName,Context context) {
Intent shortIntent = new Intent();shortIntent.setAction(ACTION_UNINSTALL_SHORTCUT);shortIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);shortIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);context.sendBroadcast(shortIntent);
}
}