如何为Android App 添加快捷方式

概述

效果

在这里插入图片描述

好处

  • 可以方便用户直接在桌面跳到目标页面
  • 可以让你的App显得更加专业(不过切忌添加过多,1-3个就够了)

添加方式

静态添加

  • 在资源文件中添加 xml 目录,并在里面创建 shortcuts.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/icon1"
        android:shortcutDisabledMessage="@string/static_message"
        android:shortcutId="static1"
        android:shortcutLongLabel="@string/static_long_label_1"
        android:shortcutShortLabel="@string/static_short_label_1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.test.shortcutsdemo.StaticTestActivity1"
            android:targetPackage="com.test.shortcutsdemo" />
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@mipmap/icon2"
        android:shortcutDisabledMessage="@string/static_message"
        android:shortcutId="static2"
        android:shortcutLongLabel="@string/static_long_label_2"
        android:shortcutShortLabel="@string/static_short_label_2">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.test.shortcutsdemo.StaticTestActivity2"
            android:targetPackage="com.test.shortcutsdemo" />
    </shortcut>

</shortcuts>
  • AndroidManifest.xml 文件中
<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
        </activity>

动态添加

  • 构造ShortcutInfo
val shortcutList = mutableListOf<ShortcutInfo>()
val shortcut1 = ShortcutInfo.Builder(this, "Dynamic1").apply {
                setShortLabel("动态快捷1")
                setLongLabel("D1")
                setIcon(Icon.createWithResource(this@MainActivity, R.mipmap.icon3))
                setIntent(Intent().apply {
                    action = Intent.ACTION_MAIN
                    setClass(this@MainActivity, DynamicTestActivity::class.java)
                    putExtra("info", "Dynamic shortcuts target class with intent1")
                })
            }.build()
            shortcutList.add(shortcutList);
  • 添加到系统中
            shortcutManager.dynamicShortcuts = shortcutList
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值