Android开发笔记[11]-长按图标显示快捷方式

摘要

配置长按图标的快捷方式.

关键信息

  • Android Studio:Iguana | 2023.2.1
  • Gradle:distributionUrl=https://services.gradle.org/distributions/gradle-8.4-bin.zip
  • jvmTarget = '1.8'
  • minSdk 21
  • targetSdk 34
  • compileSdk 34
  • 开发语言:Kotlin,Java
  • ndkVersion = '21.1.6352462'
  • kotlin版本:1.9.20
  • kotlinCompilerExtensionVersion '1.5.4'
  • com.android.library:8.3

原理简介

长按图标快捷方式配置

[https://juejin.cn/post/7095926995032211492]
[https://gitee.com/c6209/android-shortcuts]
长按桌面图标展示快捷方式,今时看来,早已司空见惯,一是Android很早的版本就已经支持,二是大部分的应用也已经实现,像微信,支付宝,头条等,所以无论功能还是实现方式,都已经踊跃出了大量的技术博文,但细细看去,却很少有一个统一的流程及具体的实现方案,本文针对此功能做了细致的总结,一是,便于日后开发的需要,二是,希望可以帮助到有类似需求的小伙伴。
这个特性,可以追溯到Android 7.1,也就是在7.1之后的系统,如果app支持,可以通过长按app图标展示一些快捷操作.
shortcuts.xml

  1. 配置快捷方式
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <!--
        android:enabled  shortcut是否可用
        android:icon  快捷图标
        android:shortcutId  快捷方式唯一的id
        android:shortcutShortLabel  短名称
        android:shortcutLongLabel  这里是配置的长名称, launcher会优先选择长名称显示,显示不下会选择短名称

        android:action    android.intent.action.VIEW
        android:targetClass  要跳转到哪个目标类
        android:targetPackage  指定一个目标应用的包名
        categories  android.shortcut.conversation
    -->
    <shortcut
        android:enabled="true"
        android:icon="@drawable/mine_img"
        android:shortcutId="MyMine"
        android:shortcutShortLabel="@string/mine">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.harry.shortcuts.MineActivity"
            android:targetPackage="com.harry.shortcuts" />
        <categories android:name="android.shortcut.conversation" />
        <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@drawable/collection_img"
        android:shortcutId="MyCollection"
        android:shortcutShortLabel="@string/collection">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.harry.shortcuts.CollectionActivity"
            android:targetPackage="com.harry.shortcuts" />
        <categories android:name="android.shortcut.conversation" />
        <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
    </shortcut>
</shortcuts>
  1. 清单文件AndroidManifest.xml里进行配置,这个需要注意一下:只能在有action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER的Activity中配置才有效,说简单点,也就是应用的主入口。
<meta-data android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts"/>

实现

核心代码

AndroidManifest.xml

<!-- 启动页 -->
<activity
    android:name=".BootActivity"
    android:exported="true"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" >

    <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>

shortcut.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <!--
        android:enabled  shortcut是否可用
        android:icon  快捷图标
        android:shortcutId  快捷方式唯一的id
        android:shortcutShortLabel  短名称
        android:shortcutLongLabel  这里是配置的长名称, launcher会优先选择长名称显示,显示不下会选择短名称

        android:action    android.intent.action.VIEW
        android:targetClass  要跳转到哪个目标类
        android:targetPackage  指定一个目标应用的包名
        categories  android.shortcut.conversation
    -->

    <!-- 快捷方式:知识卡片 -->
    <shortcut
        android:enabled="true"
        android:icon="@drawable/shortcut_card"
        android:shortcutId="ShortcutCard"
        android:shortcutShortLabel="@string/shortcut1_name">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="cn.qsbye.alittlesmile_android.BootActivity"
            android:targetPackage="cn.qsbye.alittlesmile_android" />
        <categories android:name="android.shortcut.conversation" />
        <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
    </shortcut>

    <!-- 快捷方式: 智慧视觉 -->
    <shortcut
        android:enabled="true"
        android:icon="@drawable/shortcut_smart_vision"
        android:shortcutId="ShortcutSmartVision"
        android:shortcutShortLabel="@string/shortcut2_name">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="cn.qsbye.alittlesmile_android.BootActivity"
            android:targetPackage="cn.qsbye.alittlesmile_android" />
        <categories android:name="android.shortcut.conversation" />
        <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
    </shortcut>

    <!-- 快捷方式: lofi空间 -->
    <shortcut
        android:enabled="true"
        android:icon="@drawable/shortcut_relax"
        android:shortcutId="ShortcutLofiSpace"
        android:shortcutShortLabel="@string/shortcut3_name">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="cn.qsbye.alittlesmile_android.BootActivity"
            android:targetPackage="cn.qsbye.alittlesmile_android" />
        <categories android:name="android.shortcut.conversation" />
        <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
    </shortcut>

</PreferenceScreen>

效果

长按图标显示快捷方式
posted @ 2024-03-17 10:41  qsBye  阅读(65)  评论(0编辑  收藏  举报