首页 > 其他分享 >Android开发笔记[11]-长按图标显示快捷方式

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

时间:2024-03-17 10:57:01浏览次数:23  
标签:11 xml https Android 快捷方式 android 图标

摘要

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

关键信息

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

效果

长按图标显示快捷方式

标签:11,xml,https,Android,快捷方式,android,图标
From: https://www.cnblogs.com/qsbye/p/18078252

相关文章

  • Android开发笔记[10]-关于页
    摘要构建关于页、最终用户许可页(EULA)页和隐私协议页;Compose页面中嵌入xml布局;Compose页面中添加markdown文本.关键信息AndroidStudio:Iguana|2023.2.1Gradle:distributionUrl=https://services.gradle.org/distributions/gradle-8.4-bin.zipjvmTarget='1.8'minSdk......
  • 工作总结!日志打印的11条建议
    前言大家好,我是JavaPub。日志是我们定位问题的得力助手,也是我们团队间协作沟通(甩锅)、明确责任归属(撕B)的利器。没有日志的程序运行起来就如同脱缰的野......
  • langchain Chatchat windows11 CPU部署记录
    1、下载langchainChatchatgitclonehttps://github.com/chatchat-space/Langchain-Chatchat.git(可选)如果下载不下来,可能使用了代理,需要设置代理地址:gitconfig--globalhttp.proxy"localhost:你的代理端口"之后可以取消代理设置:gitconfig--global--unsethttp.proxy2......
  • windows11配置ssh别名实现免密登录linux系统
    1在windows11上安装git2在git终端输入以下命令生成id_rsa进入到.ssh目录C:\Users\xxxxx.ssh,一般情况目录在这个位置ssh-keygen-trsa-b4096-C"[email protected]"输入命令后一直回车生成3将生成的rsa_pub的内容复制到linux服务器的~/用户名/.ssh/authorized_keys中......
  • 初级Android开发学习笔记(三)图形用户界面设计-按钮Button
      按钮Button及按钮处理事件        按钮Button用于处理人机交互的事件,在一般应用程序中常常会用到。由于按钮Button是文本标签TextView的子类,按钮Button继承了文本标签TextView所有的方法和属性。其继承关系如图所示。    按钮Button在程序设计......
  • P1116 车厢重组 洛谷
    附加AC代码噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢哦哦哦!#车厢重组##题目描述在一个旧式的火车站旁边有一座桥,其桥面可以绕河中心的桥墩水平旋转。一个车站的职工发现桥的长度最多能容纳两节车厢,如果将桥旋转180 度,则可以把相邻两节车厢的位置交换,用这种方法可以重新排列车......
  • 0基础 三个月掌握C语言(11)
    字符函数和字符串函数为了方便操作字符和字符串C语言标准库中提供了一系列库函数接下来我们学习一下这些函数字符分类函数C语言提供了一系列用于字符分类的函数,这些函数定义在ctype.h头文件中。这些函数通常用于检查字符是否属于特定的类别,例如大写字母、小写字母、数字......
  • Win11复现 PointNet ++ 分类任务 || 三维点云深度学习第一篇
    ​Windows11Github上复现PointNet++Classification任务下载代码和安装依赖环境安装git知乎教程Gitclone源码地址:PointNetGithub点击Code复制HTTPS到目标目录shift-右键GitBashHere输入GitClone<url>​​​​​​安装nodejsNode.js—Down......
  • P4211 [LNOI2014] LCA 题解
    link切入这道题,首先要思考所有LCA的分布特征。显然,对于任意\(\text{LCA}(i,j)\),都满足LCA是\(i,j\)的祖先。那么对于一个询问,可以找到所有\(i\in[l,r]\)的祖先,还可以找所有\(z\)的祖先。明显,找\(z\)的祖先会方便很多:它们都分布在\(z\)到根节点的那条链上,这应......
  • chapter11-图论
    1.图的存储方式首先,关于图的存储方式有2种,一种是邻接矩阵,一种是邻接表,而邻接表适用于1个点对到其他所有点的批处理,实际程序中经常使用。邻接表会给每一个顶点建立一个单链表,即使那个顶点没有度(无向图),or没有任何出度(有向图)。在程序中,我们并不是使用单链表来存储,而是一个向量数组......