Android ShortCuts使用

article/2025/8/17 20:26:45

ShortCuts是什么?

Shortcuts是指在桌面长按app图标而出现的快捷方式,可以为你的app的关键功能添加更加快速的入口而不是先打开app这里写图片描述

点击快捷方式可以访问应用功能,而且这种快捷方式也可以被拖拽到桌面的单独位置,变成单独的左面快捷方式。
这里写图片描述

添加ShortCuts的方式

有两种方式:
1.静态方式:在xml中定义,适用于一些通用操作
2.动态方式:由ShortcutManager发布,可以根据用户的行为或者偏好添加,可以动态更新

一些限制条件:
1.每个应用目前最多可以有5个shortcuts(包括静态和动态)
2.shortcuts是android7.1(api25)的api,所以只能在android7.1的设备上才能正常显示,同时需要launcher支持,nexus系列手机和pixel系列手机已经支持了。

动态添加方式

动态添加方式比较灵活,可以适用的场景比较丰富,一般开发过程中用到的机会比较多。这种方式可以再用户适用app的过程中构建,更新,或者删除。

-发布:setDynamicShortcuts,addDynamicShortcuts
-更新:updateShortcuts
-删除:removeDynamicShortcuts,removeAllDynamicShortcuts

ShortcutManager还有其他操作方法,代码如下:

 public boolean setDynamicShortcuts(List<ShortcutInfo> shortcutInfoList) {throw new RuntimeException("Stub!");}public List<ShortcutInfo> getDynamicShortcuts() {throw new RuntimeException("Stub!");}public List<ShortcutInfo> getManifestShortcuts() {throw new RuntimeException("Stub!");}public boolean addDynamicShortcuts(List<ShortcutInfo> shortcutInfoList) {throw new RuntimeException("Stub!");}public void removeDynamicShortcuts(List<String> shortcutIds) {throw new RuntimeException("Stub!");}public void removeAllDynamicShortcuts() {throw new RuntimeException("Stub!");}public List<ShortcutInfo> getPinnedShortcuts() {throw new RuntimeException("Stub!");}public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {throw new RuntimeException("Stub!");}public void disableShortcuts(List<String> shortcutIds) {throw new RuntimeException("Stub!");}public void disableShortcuts(List<String> shortcutIds, CharSequence disabledMessage) {throw new RuntimeException("Stub!");}public void enableShortcuts(List<String> shortcutIds) {throw new RuntimeException("Stub!");}public int getMaxShortcutCountPerActivity() {throw new RuntimeException("Stub!");}public boolean isRateLimitingActive() {throw new RuntimeException("Stub!");}public int getIconMaxWidth() {throw new RuntimeException("Stub!");}public int getIconMaxHeight() {throw new RuntimeException("Stub!");}public void reportShortcutUsed(String shortcutId) {throw new RuntimeException("Stub!");}

demo代码:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this,"id1").setShortLabel("short label1").setLongLabel("long label1").setRank(4).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this,"id2").setShortLabel("short label2").setLongLabel("long label2").setRank(3).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();ShortcutInfo shortcut3 = new ShortcutInfo.Builder(this,"id3").setShortLabel("short label3").setLongLabel("long label3").setRank(2).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();ShortcutInfo shortcut4 = new ShortcutInfo.Builder(this,"id4").setShortLabel("short label4").setLongLabel("long label4").setRank(1).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();ShortcutInfo shortcut5 = new ShortcutInfo.Builder(this,"id5").setShortLabel("short label5").setLongLabel("long label5").setRank(2).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut1,shortcut2,shortcut3,shortcut4,shortcut5));

注:关于shortcuts数量的问题,虽然名义上能添加5个,添加第6个的时候会抛出异常,但是只会显示4个,用setRank能改变显示的顺序(从小到大,不支持负数,数越小,离icon约近)

使用过程中处理back的问题,即多个Intent构建back stack的问题

ShortcutInfo shortcut5 = new ShortcutInfo.Builder(this,"id5").setShortLabel("short label5").setLongLabel("long label5").setRank(2).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntents(new Intent[]{new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),new Intent(Main2Activity.ACTION_SS)}).build();
<activity
            android:name=".Main2Activity"android:label="@string/title_activity_main2"android:theme="@style/AppTheme.NoActionBar"><intent-filter><action android:name="com.dahuatech.shortcuts.ddd"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity>

注:target activity必须要用action启动

静态方式

  <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>
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcut
        android:enabled="true"android:icon="@drawable/ic_check_circle_black_24dp"android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"android:shortcutId="static"android:shortcutLongLabel="@string/static_shortcut_long_label_1"android:shortcutShortLabel="@string/static_shortcut_short_label_1"><intent
            android:action="android.intent.action.VIEW"android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"android:targetPackage="com.ddmeng.hellonougat" /></shortcut><shortcut
        android:enabled="true"android:icon="@drawable/ic_android_black_24dp"android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"android:shortcutId="static_2"android:shortcutLongLabel="@string/static_shortcut_long_label_2"android:shortcutShortLabel="@string/static_shortcut_short_label_2"><intent
            android:action="android.intent.action.MAIN"android:targetClass="com.ddmeng.hellonougat.MainActivity"android:targetPackage="com.ddmeng.hellonougat" /><intent
            android:action="com.ddmeng.hellonougat.action.STATIC_SHORTCUT_2"android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"android:targetPackage="com.ddmeng.hellonougat" /></shortcut>
</shortcuts>

上面这个文件里添加了两个静态的shortcuts, 第一个关联了一个Activity, 点击shortcut将直接打开这个Activity, 回退的时候回到桌面。


http://chatgpt.dhexx.cn/article/KYh3uHi2.shtml

相关文章

Android App Shortcuts

周五了&#xff0c;周末还远吗&#xff1f;~ 加入我知识星球的人都得到了提升&#xff0c;可不要错过哦~ 点击阅读原文即可。 本文较长&#xff0c;阅读大约十分钟 ~ 我们都知道iPhone6S推出了3D Touch的功能&#xff0c;也算是一个大卖点&#xff0c;Google也后来居上&#xf…

Linux下cut命令用法详解

Linux下cut命令用法详解 原创&#xff1a;frozen_sucker(冰棍) 有时我们经常会遇到这样一些问题&#xff1a;有一页电话号码薄&#xff0c;上面按顺序规则地写着人名、家庭住址、电话、备注等&#xff0c;此时我们只想取出所有人的名字和其对应的电话号码&#xff0c;你有几种…

使用Dynamic Shortcuts

在看完Static Shortcuts后, 我们不相信Google仅仅给我们开发者开放了局限性如此大的使用方式, 肯定还会存在灵活性更大的API, 是的, 这就是我们马上要讲的Dynamic Shortcuts, 我把它称为动态配置. 说起动态配置, 那肯定是用java代码实现了, 那如何实现呢? 首先第一步, 我们需…

Shortcuts,让你可以在系统的桌面上为所欲为

/ 今日科技快讯 / 近日&#xff0c;有用户刷抖音时看到烤虾广告下单&#xff0c;实付款194元买了250克烤虾干&#xff0c;货到付款后发现是三无产品&#xff0c;且外形口感与广告宣传天差地别。对此抖音表示&#xff0c;已对此事展开相关调查&#xff0c;并下架涉嫌虚假宣传…

Shortcuts使用解析(一)

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/susan_123456/article/details/93793490 Shortcuts是什么&#xff1f; Shortcuts是指在桌面长按App图标而出现的快捷方式&#xff0c;可以为你的App的关键功能添加更快速的…

是时候来了解android7了:shortcuts(快捷方式)

本文已授权微信公众号&#xff1a;鸿洋&#xff08;hongyangAndroid&#xff09;在微信公众号平台原创首发。 就在前几天的一个晚上, Google召开了它的秋季发布会, 毫无悬念的宣布了它的最新手机品牌Pixel, 与此同时我的nexus设备也从亲儿子降级成为干儿子. 不过还好Google并没…

Android 7.1 新特性:快捷方式 Shortcuts 详解

Android 7.1 新特性&#xff1a;快捷方式 Shortcuts 详解 一、Shortcuts 介绍 Android 7.1 允许 App 自定义 Shortcuts&#xff0c;类似 iOS 的 3D touch。通过在桌面长按 App 弹出 Shortcut 列表&#xff0c;点击某个 Shortcut 快速进入某项操作&#xff0c;同时 Shortcut 可以…

Android7.1新特性:快捷方式Shortcuts详解

Shortcuts介绍 Android7.1&#xff08;API Level 25&#xff09;及以上系统可以自定义Shortcuts&#xff0c;通过在桌面上长按App Icon弹出Shortcut列表&#xff0c;点击某个shortcut可使用户快捷得打开App里常用的或推荐的任务。长按列表中的任一个Shortcut可拖动到桌面进行固…

oracle 左连接

oracle左连接小结 当使用左连接关联表进行查询时&#xff0c;如果右侧的表在关联的条件中有重复的数据&#xff0c;就会导致最后的查询结果比预期的多&#xff0c;比如&#xff1a; select * from A 需要关联的表 select * from B 这里的两个表的id都是主键&#xff0c;现在…

lambda多表左连接

下面是mysql多表左连接&#xff0c;对应的lambda表达式写法&#xff0c;只为快速解决你的问题&#xff0c;详细的就不介绍了&#xff0c;只出结果&#xff0c;相信稍微有点基础的都能看懂。相关知识点&#xff0c;请自行百度 关键字&#xff1a;GroupJoin、SelectMany sql语句…

实例讲解内连接、左连接、右连接、交叉连接、外连接以及全连接

目录 示例表&#xff1a;1.内连接-inner&#xff1a;实例1&#xff1a;内连接表a和表b实例2&#xff1a;内连接表a和表c实例3&#xff1a;内连接表a和表b,使用“>”号实例4&#xff1a;内连接表a和表b,使用“<”号实例5&#xff1a;内连接表a和表b,指定字段显示实例6&…

mysql 左连接查询记录数,MySQL左连接查询记录行数

最近修复一个数据问题&#xff0c;其中用到了左连接&#xff0c;我的意识里还以为A左连接B&#xff0c;查询出的记录数量会是A的记录数&#xff0c;然而实际操作后发现认知严重错。 网上关于左连接的一张图 摘自网上的一张图&#xff0c;图确实没毛病&#xff0c;但我却产生了误…

mysql左连接出现笛卡尔积_Mysql内连接、左连接会出现笛卡尔积的理解

先简单解释一下笛卡尔积。 现在,我们有两个集合A和B。 A = {0,1} B = {2,3,4} 集合 AB 和 BA的结果集就可以分别表示为以下这种形式: AB = {(0,2),(1,2),(0,3),(1,3),(0,4),(1,4)}; BA = {(2,0),(2,1),(3,0),(3,1),(4,0),(4,1)}; 以上AB和BA的结…

关于左右连接

首先来看一下两张主要的表&#xff1a; persons表 orders表 现在我们希望列出所有的人&#xff0c;以及他们的定购。 SELECTpersons.last_name,persons.first_name,orders.order_no FROMpersons LEFT JOIN orders ON persons.pid orders.pid ORDER BYpersons.last_nam…

LINQ左连接

title: linq左连接 date: 2021-12-05 08:45:48 tags: C# Linq LINQ语言继承查询 近日项目实践中遇到这样的一个事情。在一个项目需求里存在这么一个情况&#xff0c;有以下一个主表与几个基础数据表组成一个大表&#xff0c;要求主表中的有的数据必须展示出来&#xff0c;但大…

ClickHouse左连接

A左连接B>S1区域 1、等价方式一 SELECT date, ts, country, province, city,ye_no FROM (SELECT date, ts, country, province, city,0 AS yes_no FROM DBa.tba WHERE (date today()-1) ) ALL LEFT JOIN (SELECT date, ts, country, province, city FROM DBb.tbb WH…

【Mysql】图解左、右、内与全连接

一、前言 使用学生表与成绩表来演示Mysql中的各种连接查找 学生表的建表语句如下&#xff1a; CREATE TABLE student(id int(11) NOT NULL AUTO_INCREMENT COMMENT 自增序号,st_id int(11) DEFAULT NULL COMMENT 学生id,st_name varchar(255) DEFAULT NULL COMMENT 学生姓名…

使用VC和MATCOM结合开发应用程序心得笔记

使用VC和MATCOM结合开发应用程序心得笔记1. 什么是Matcom?Matcom是MathTools公司(UPenn)开发的一个基于C矩阵函数库Matrix的一个matlab m文件与cpp文件的转换程序。MIDEVA为他的一个集成调试编译环境(界面)。Matcom独立于matlab之外&#xff0c;但需要外部的c编译器&…

MATLAB与VC混合编程(1)

在本节中先介绍matcom&#xff0c;使得VC能调用Matlab 的M文件&#xff0c;从而实现混合编程。 Matcom是mathTools公司推出的一个能将M文件转化成相同功能C代码的工具。相比Matlab自带的编译器Compiler&#xff0c;用Matcom转化代码要简单和方便得多。以VC6.0和Matcom为例&…

利用Matcom实现基于MATLAB的混合编程

利用Matcom实现基于MATLAB的混合编程 来源: 神经网络 作者&#xff1a;AI俱乐部 网友评论 0 条 浏览次数 42 <script> remark_look(re_look.php?cid17&tid1843); </script> Matcom是mathTools公司推出的最早的Matlab到C的编译器。后来&#xff0c;由于Mathto…