IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> android app图标长按展示快捷方式 Shortcuts 类似3d touch -> 正文阅读

[移动开发]android app图标长按展示快捷方式 Shortcuts 类似3d touch

android 长按图标展示快捷方式


首先,这不是一个新功能了,在android 7.1的时候就已经支持创建快捷入口了。因为当时项目用不到也没关注过这块,现在很多应用都支持长按展示快捷入口了,确实有必要对这块了解下了。

1. 静态创建

静态创建比较适合常用的快捷方式不怎么变的情况。

  • AndroidManifest.xml清单文件中找到应用程序的入口,也就是 intent过滤器设置为 android.intent.action.MAIN 操作和 android.intent.category.LAUNCHER 类别的 Activity
  • 向此 activity 添加 <meta-data>元素,引用定义了快捷方式的资源文件。
<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <!--引入shortcuts资源-->
    <meta-data
        android:name="android.app.shortcuts"
        android:resource="@xml/shortcuts" />
</activity>
  • res/xml下创建shortcuts.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:shortcutId="money_in" 
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/short_money_in">
        <!--        android:shortcutLongLabel="@string/compose_shortcut_long_label1"-->
        <!--        android:shortcutDisabledMessage="@string/compose_disabled_message1">-->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.appshotcutssimple"
            android:targetClass="com.example.appshotcutssimple.MoneyInActivity" />
        <!-- If your shortcut is associated with multiple intents, include them
             here. The last intent in the list determines what the user sees when
             they launch this shortcut. -->
        <categories android:name="android.shortcut.conversation" />
        <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
    </shortcut>

    <shortcut
        android:shortcutId="money_out"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/short_money_out">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.appshotcutssimple"
            android:targetClass="com.example.appshotcutssimple.MoneyOutActivity" />
        <!-- If your shortcut is associated with multiple intents, include them
             here. The last intent in the list determines what the user sees when
             they launch this shortcut. -->
        <categories android:name="android.shortcut.conversation" />
        <capability-binding android:key="actions.intent.CREATE_MESSAGE" />
    </shortcut>
</shortcuts>

其中 shortcutId 为id
shortcutShortLabel为简短说明,也就是名字
icon 为图标
categories 为应用程序的快捷方式执行的操作类型提供分组,例如创建新的聊天消息
capability-binding 可选 声明与此快捷方式关联的功能。CREATE_MESSAGE 声明的功能,是与应用有关的 Action 内置 intent。用户可以结合使用语音指令与 Google 助理来调用此快捷方式。

来看下最终的效果:
在这里插入图片描述

2. 动态创建

  • 动态创建的代码
binding.btnAdd.setOnClickListener {
    shortScan = ShortcutInfoCompat.Builder(this, "shortcut_scan")
        .setShortLabel(getString(R.string.short_scan))
        .setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
        .setIntent(Intent(Intent.ACTION_MAIN, null, this, MainActivity::class.java))
        .build()
    ShortcutManagerCompat.addDynamicShortcuts(this, mutableListOf(shortScan))

    Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show()
}
  • 动态移除
binding.btnRemove.setOnClickListener {
    ShortcutManagerCompat.removeDynamicShortcuts(
        this@MainActivity,
        Collections.singletonList("shortcut_scan")
    )

    Toast.makeText(this, "移除成功", Toast.LENGTH_SHORT).show()
}

这样就可以动态实现添加快捷入口的功能。
最后附上项目地址: 点击此处

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-09 10:21:10  更:2021-08-09 10:22:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 12:32:40-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码