Android 桌面图标长按快捷入口(Shortcut、7.1.1、API25+)

8 篇文章 0 订阅
5 篇文章 0 订阅

Android 桌面图标长按快捷入口(Shortcut、7.1.1、API25+)

Android 7.1.1(API 25)以后支持的功能,在桌面长按应用图标,可以设置快捷方式。

静态设置

res -> xml-v25 下创建 shortcuts 配置文件,例如: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/ic_launcher"
        android:shortcutDisabledMessage="@string/app_name"
        android:shortcutId="id1"
        android:shortcutLongLabel="@string/app_name"
        android:shortcutShortLabel="@string/app_name">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="${启动的Activity,例如:com.xxx.MainActivity}"
            android:targetPackage="${你的包名}" />

        <categories android:name="android.shortcut.conversation" />
    </shortcut>

</shortcuts>

AndroidManifest.xml 中添加shortcuts.xml配置,要添加在桌面图标Activity标签下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxxx">
    ...
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ShortcutsSample">
        <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>
    </application>

</manifest>

完成

动态设置

初始化管理器

private val mShortcutManager: ShortcutManager? = context.getSystemService(ShortcutManager::class.java)

获取快捷入口最大可添加数量

// 获取可设置快捷图标的最大数量
val maxShortcutCount = mShortcutManager?.maxShortcutCountPerActivity ?: 0

添加快捷入口(伪代码)

需要注意的是,在添加快捷入口的时候,要判断数量不能超过可以添加的最大数量,如果超过最大数量会报错。

val shortcutList = ArrayList<ShortcutInfo>()

val shortcutInfo1 = ShortcutInfo.Builder(context, shortcutEntity.id)
            .setShortLabel(shortcutEntity.content)
            .setLongLabel(shortcutEntity.content)
            // intent 可以打开某个app,或者打开其他意图
            .setIntent(intent)
            	// 显示一个Iocn
            // .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
            .setIcon(icon)
            .build()
...
shortcuts.add(shortcutInfo1)
shortcuts.add(shortcutInfo2)
shortcuts.add(shortcutInfo3)
// 可添加多个
...

// 获取可设置快捷图标的最大数量
val maxShortcutCount = mShortcutManager?.maxShortcutCountPerActivity ?: 0

if (shortcutList.size > maxShortcutCount) {
    mShortcutManager?.dynamicShortcuts = shortcutList.subList(0, maxShortcutCount)
} else {
    mShortcutManager?.dynamicShortcuts = shortcutList
}

获取已经保存到桌面的快捷入口

val pinnedShortcuts = mShortcutManager?.pinnedShortcuts

设置快捷入口为不可用状态

val pinnedShortcuts = mShortcutManager?.pinnedShortcuts
val shortcutIds = ArrayList<String>()
pinnedShortcuts?.forEach { shortcutInfo -> shortcutIds.add(shortcutInfo.id) }
mShortcutManager?.disableShortcuts(shortcutIds, "打样了")

移除全部快捷入口

mShortcutManager?.removeAllDynamicShortcuts()

this all

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值