快捷方式的添加与删除

来源:互联网 发布:Ubuntu设置软件源 编辑:程序博客网 时间:2024/05/22 11:31

快捷方式的添加与删除都是通过广播来实现

添加快捷方式:
private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));//快捷方式的名称
shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);//是否允许重复创建快捷方式
Intent intent = new Intent();
intent.setComponent(new ComponentName(this.getPackageName(),".XxxActivity"));//设置要激活的Activity
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);//点击快捷方式后发送一个Intent
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this,  R.drawable.icon));
sendBroadcast(shortcutIntent);

权限:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

删除快捷方式:
Intent intent = new Intent(DELETE_ACTION);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Sample");
ComponentName comp = new ComponentName("com.example.android.apis","com.example.android.apis.app.LauncherShortcuts");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent(comp).setAction("android.intent.action.MAIN"));
sendBroadcast(intent);


权限:
<uses-permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />