Android 桌面快捷方式

Android 快捷方式

在这里插入图片描述

使用方式

ShortcutUtils.getInstance().addShortcut(this
                    		 , MainActivity2.class
                   			 , liveBundle
                   			 , "live_Id"
                   			 , "看直播"
                   			 , "看直播"
                    		, R.drawable.live)
                    .addShortcut(this
                            , MainActivity2.class
                            ,vodBundle
                            , "vod_Id"
                            , "看回放"
                            , "看回放"
                            , R.drawable.vod)
                    .build();

添加权限

    <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" /> <!-- 添加快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <!-- 移除快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <!-- 查询快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS" />
    <uses-permission android:name="com.android.launcher2.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS" />
    <uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS" />
    <uses-permission android:name="org.adw.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="org.adw.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.htc.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.qihoo360.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.qihoo360.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.lge.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.lge.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="net.qihoo.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="net.qihoo.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="org.adwfreak.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="org.adwfreak.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="org.adw.launcher_donut.permission.READ_SETTINGS" />
    <uses-permission android:name="org.adw.launcher_donut.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.huawei.launcher3.permission.READ_SETTINGS" />
    <uses-permission android:name="com.huawei.launcher3.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.fede.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.fede.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.sec.android.app.twlauncher.settings.READ_SETTINGS" />
    <uses-permission android:name="com.sec.android.app.twlauncher.settings.WRITE_SETTINGS" />
    <uses-permission android:name="com.anddoes.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.anddoes.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.tencent.qqlauncher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.tencent.qqlauncher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.huawei.launcher2.permission.READ_SETTINGS" />
    <uses-permission android:name="com.huawei.launcher2.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.android.mylauncher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.android.mylauncher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.ebproductions.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.ebproductions.android.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.oppo.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.oppo.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS" />
    <uses-permission android:name="telecom.mdesk.permission.READ_SETTINGS" />
    <uses-permission android:name="telecom.mdesk.permission.WRITE_SETTINGS" />
    <uses-permission android:name="dianxin.permission.ACCESS_LAUNCHER_DATA" />

ShortcutUtils工具类(直接使用)

/**
 * Android 快捷方式工具类
 * Build.VERSION.SDK_INT >= 25 * API25以上可用
 * setShortLabel 设置短标题
 * setLongLabel  设置长标题
 * setIcon       设置icon
 * setIntent     设置Intent
 * @author renquan
 * @date 2021年12月27日
 */
public class ShortcutUtils {
    private static ShortcutUtils shortcutUtils;
    private List<ShortcutInfo> shortcutInfos;
    private Context mContext;

    public static ShortcutUtils getInstance() {
        if (shortcutUtils == null) {
            synchronized (ShortcutUtils.class) {
                if (shortcutUtils == null) {
                    shortcutUtils = new ShortcutUtils();
                }
            }
        }
        return shortcutUtils;
    }

    private ShortcutUtils() {
        shortcutInfos = new ArrayList<>();
    }

    /**
     * 设置Class对象
     * 所有参数不能为空
     * @param context
     * @param cls
     * @param bundle
     * @param shortcutId
     * @param shortLabel
     * @param longLabel
     * @param resId
     * @return
     */
    public ShortcutUtils addShortcut(Context context, Class<?> cls, Bundle bundle, String shortcutId, String shortLabel, String longLabel, @DrawableRes int resId) {
        if (shortcutUtils != null && shortcutInfos != null) {
            if (Build.VERSION.SDK_INT >= 25) {
            	mContext = context;
                Intent intent = new Intent(context, cls);
                intent.putExtra("shortcutArgument", bundle);
                intent.setAction(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, shortcutId)
                        .setShortLabel(shortLabel)
                        .setLongLabel(longLabel)
                        .setIcon(Icon.createWithResource(context, resId))
                        .setIntent(intent)
                        .build();

                shortcutInfos.add(shortcutInfo);
            }
        }
        return shortcutUtils;
    }


    /**
     * 设置Intent对象
     * 所有参数不能为空
     * @param context
     * @param intent
     * @param shortcutId
     * @param shortLabel
     * @param longLabel
     * @param resId
     * @return
     */
    public ShortcutUtils addShortcut(Context context, Intent intent, String shortcutId, String shortLabel, String longLabel, @DrawableRes int resId) {
        if (Build.VERSION.SDK_INT >= 25) {
            mContext = context;
            if (shortcutUtils != null && shortcutInfos != null) {
                ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, shortcutId)
                        .setShortLabel(shortLabel)
                        .setLongLabel(longLabel)
                        .setIcon(Icon.createWithResource(context, resId))
                        .setIntent(intent)
                        .build();

                shortcutInfos.add(shortcutInfo);
            }
        }
        return shortcutUtils;
    }

    /**
     * 构建Shortcuts
     */
    public void build() {
        if (shortcutInfos != null && shortcutInfos.size() > 0 && mContext != null) {
            ShortcutManager systemService = mContext.getSystemService(ShortcutManager.class);
            systemService.setDynamicShortcuts(shortcutInfos);
        }
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值