Android创建/删除桌面快捷方式

创建桌面快捷方式,老的代码就不贴了,网上一大堆,基本是intent设置各种action。从Android 7.1(API 25)开始,Android新增了ShortcutManager,可以对桌面久按应用图标弹出的快捷方式进行管理,但是API 25上仍然可以使用老的方式添加快捷方式,从API 26开始支持通过ShortcutManager添加快捷方式了。

为了兼容低版本我们可以使用support提供的方法,support-v4中有一个ShortcutManagerCompat类可以兼容处理,我直接封装了工具类方法:

    /**
     * 添加桌面快捷方式
     * @param context
     * @param className 快捷方式的目标类(全包名的路径)
     * @param id        快捷方式对应的id
     * @param iconResId 快捷方式的图标资源
     * @param labelResId 快捷方式的名称资源
     */
    public static void addShortCutCompat(Context context, String className, String id, int iconResId, int labelResId) {
        Intent shortcutInfoIntent = new Intent();
        shortcutInfoIntent.setClassName(context, className);
        shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
        addShortCutCompat(context, shortcutInfoIntent, id, iconResId, labelResId);
    }

    /**
     * 添加桌面快捷方式
     * @param context
     * @param shortcutInfoIntent 点击快捷方式时的启动目标intent
     * @param id        快捷方式对应的id
     * @param iconResId 快捷方式的图标资源
     * @param labelResId 快捷方式的名称资源
     */
    public static void addShortCutCompat(Context context, Intent shortcutInfoIntent, String id, int iconResId, int labelResId) {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
            ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(context, id)
                    .setIcon(IconCompat.createWithResource(context, iconResId))
                    .setShortLabel(context.getResources().getString(labelResId))
                    .setIntent(shortcutInfoIntent)
                    .build();
            //这里第二个参数可以传一个回调,用来接收当快捷方式被创建时的响应
            ShortcutManagerCompat.requestPinShortcut(context, info, null);
        }
    }

    /**
     * 删除快捷方式
     * @param context
     * @param shortCutTitle 快捷方式的名称
     * @param className 快捷方式的目标类(全包名的路径)
     */
    public static void deleteShortCut(Context context, String shortCutTitle, String className) {
        Intent shortcutIntent =  new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setComponent(new ComponentName(context.getPackageName(), className));
        Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
        intent.putExtra("android.intent.extra.shortcut.NAME", shortCutTitle);
        intent.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent);
        intent.putExtra("duplicate", true);
        context.sendBroadcast(intent);
    }

ShortcutManagerCompat中我没有找到删除的快捷方式的方法,所以删除还是用老的方式给Intent设置action,但是删除的方法在很多设备上目前都不起作用。。

调用:
添加快捷方式

String className = "com.test.activity.MemoActivity";
String id = className;
addShortCutCompat(getContext(), className, id, R.drawable.ic_memo_shortcut, R.string.memo);

删除快捷方式(不起作用)

String className = "com.test.activity.MemoActivity";
deleteShortCut(getContext(), getString(R.string.memo), className);

需要添加权限:

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

如果点击快捷方式启动的是一个Activity, 那么该Activity需要在AndroidManifest.xml中稍加配置:

 <application>
        <activity
            android:name=".activity.MemoActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
</application>

如果不加intent-filter中的内容的话,点击快捷方式的时候会报错,提示你要么加一个action为MAIN的intent-filter,要么设置exported为true.

参考:https://www.jianshu.com/p/18be986553db

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

川峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值