Android创建桌面快捷方式

  1. 添加桌面快捷方式
    /**
     * 创建快捷图标
     */
    private void installShortCut() {
        //先判断是否已创建了
        if (SharedPreferencesUtil.getBooleanConfig(this,
                Constants.APP_SHAREDPREFERENCE_NAME, Context.MODE_PRIVATE,
                "isCreateShortcut", false))
            return;
        SharedPreferencesUtil.setBooleanConfig(this,
                Constants.APP_SHAREDPREFERENCE_NAME, Context.MODE_PRIVATE,
                "isCreateShortcut", true);
        // 发送广播的意图, 大吼一声告诉桌面,要创建快捷图标了
        Intent intent = new Intent();
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        // 快捷方式 要包含3个重要的信息 1,名称 2.图标 3.干什么事情
        try {
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getPackageManager().getApplicationLabel(
                    getPackageManager().getApplicationInfo(getPackageName(),
                            PackageManager.GET_META_DATA)).toString());
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory
                .decodeResource(getResources(), R.drawable.ic_launcher));
//       intent.putExtra("duplicate", false); //不允许重复创建

        // 桌面点击图标对应的意图。
        Intent shortcutIntent = new Intent();
        shortcutIntent.setAction("android.intent.action.MAIN");
        shortcutIntent.addCategory("android.intent.category.LAUNCHER");
        shortcutIntent.setClassName(getPackageName(),
                "com.johnny.androiddemos.MainActivity");
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        sendBroadcast(intent);
        Toast.makeText(this, "创建桌面快捷方式成功", Toast.LENGTH_SHORT).show();
    }
  1. 加权限
 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值