Android桌面长按图标快捷方式——Shortcuts

简介

当我们在长按Android应用的桌面图标时,一般回弹出一个列表,上面一般有应用信息、卸载应用等功能,并且部分应用在这里还添加了自己的快捷方式,今天主要介绍如何添加自定义的快捷方式。

长按桌面显示的快捷方式在Android中叫Shortcut,它有两种创建方式,分为静态创建和动态创建,并且跟Widget一样,也有对应的管理类来进行管理。废话不多说,直接开始

静态创建

第一步

静态创建分两个步骤,第一个是在Manifest文件中指定快捷方式配置文件的位置,首先找到Launcher的activity标签,添加meta-data标签如下:

<activity
   android:name=".MainActivity"
    android:exported="true">
    <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>

第二步

在resources资源目录下创建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/ic_launcher_round"
        android:shortcutDisabledMessage="@string/disable_hint_message"
        android:shortcutId="shortcut1"
        android:shortcutLongLabel="@string/long_label_one"
        android:shortcutShortLabel="@string/short_label_one">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.helloworld.RecyclerViewTestActivity"
            android:targetPackage="com.example.helloworld">
            <extra
                android:name="key1"
                android:value="value1" />
        </intent>
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher_round"
        android:shortcutDisabledMessage="@string/disable_hint_message"
        android:shortcutId="shortcut2"
        android:shortcutLongLabel="@string/long_label_two"
        android:shortcutShortLabel="@string/short_label_two">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.helloworld.TestViewPagerActivity"
            android:targetPackage="com.example.helloworld">
            <extra
                android:name="key2"
                android:value="value2" />
        </intent>
    </shortcut>
</shortcuts>

<string name="short_label_one">名称一</string>
<string name="short_label_two">名称二</string>
<string name="long_label_one">一个很长很长的长到你无法相信的名称一</string>
<string name="long_label_two">一个略长略长的长到你可以想象的名称二</string>
<string name="disable_hint_message">你确认要禁用该快捷方式吗?</string>

  • enabled 是否开启
  • icon 图标
  • shortcutDisabledMessage 关闭该快捷方式提示的信息
  • shortcutId 唯一标识
  • shortcutLongLabel 长标签,优先展示
  • shortcutShortLabel 短标签,长标签展示不下会展示短标签
  • intent 跳转至Activity的Intent

注意事项

通过xml创建的静态ShortCuts不可以通过代码动态修改与删除,只能通过xml去修改。

动态创建

使用ShortCuts相关的类以及方法即可快速实现动态快捷方式的添加、更改、删除等操作。

object ShortCutManager {

    fun createShortCuts(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            ShortcutManagerCompat.removeAllDynamicShortcuts(context)
            val shortCut = ShortcutInfoCompat.Builder(context, "shortCutId")
                .setShortLabel("搜索一下")
                .setLongLabel("搜索一下下")
                .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher))
                .setIntent(Intent(context,MainActivity::class.java).apply {
                    action = Intent.ACTION_VIEW
                })
                .build()
            ShortcutManagerCompat.pushDynamicShortcut(context, shortCut)
        }
    }
}

再合适的地方调用上面的方法即可实现添加ShortCuts,在添加之前删除了所有的动态快捷方式,但是并不会删除通过xml配置的静态快捷方式。
在这里插入图片描述


---------------------
作者:Sruur
来源:CSDN
原文:https://blog.csdn.net/weixin_42643321/article/details/129854463
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件

猜你喜欢

转载自blog.csdn.net/xiaowang_lj/article/details/131805483
今日推荐