android 创建快捷方式

本文介绍如何在Android应用中创建桌面快捷方式,包括添加必要的权限、实现快捷方式的方法及配置指向不同活动的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、添加权限(必须)

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

2、添加快捷键

    public static void setupShortcut(Activity activity)
    {
        Intent shortcutIntent = new Intent(activity, MainActivity.class); //启动首页(launcher Activity)

        Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");//快捷键名字可以任意,不过最好为app名称
        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        intent.putExtra("duplicate", false);//不允许重复创建

        activity.sendBroadcast(intent);//发送广播创建快捷键
    }

3、快捷键也可以指向非Launcher activity,只需要在AndroidManifest中对应的Activity 中添加如下配置

<intent-filter>
     <action android:name="android.intent.action.CREATE_SHORTCUT" />
<intent-filter>

例如可以将2 中的MainActivity 改为任意其他Activity,同时在AndroidManifest中对应添加上述intent-filter就可以了。

转载于:https://www.cnblogs.com/lipeil/archive/2012/11/14/2769526.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值