Android 长按3Dtouch快捷方式
android 7.0新特性,类似iPhone的3Dtouch,长按app桌面图标会弹出快捷方式,效果如下:
引入快捷方式:Shortcuts
两种方式:静态xml和动态java设置(类似BroadcastReceiver)
静态使用步骤:
1.在工程资源文件res目录下新建xml/shortcuts.xml
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcutandroid:enabled="true"android:icon="@drawable/amap_car"android:shortcutDisabledMessage="@string/disable_tip"android:shortcutId="setting"android:shortcutLongLabel="@string/full_name"android:shortcutShortLabel="@string/short_name"><intentandroid:action="android.intent.action.VIEW"android:targetClass="com.lagou.ktdemo.firstkotlin.JavaActivity"android:targetPackage="com.lagou.ktdemo.firstkotlin" /><categories android:name="android.shortcut.conversation" /></shortcut></shortcuts>
shortcuts标签下可以注册多个shortcut,
shortcut的常用属性如下:
- 1.shortcutId, 唯一的id
- enabled, 表示该shortcut是否可用
- shortcutShortLabel, 短名称, 下面还会有长名称, 如果长名称显示不下, 就显示短名称
- shortcutLongLabel, 长名称, launcher会优先选择长名称显示
- shortcutDisabledMessage, 当用户选择一个不可用的shortcut时给的toast提示内容
intent表示我们点击shortcut时的意图, 属性说明:
targetPackage是指定一个目标应用的包名,
targetClass是我们要跳转的目标类,
action记得一定要配置, 否则会崩溃
categories, 目前官方只提供了android.shortcut.conversation,写死即可。
2.在清单文件中配置shortcuts:
注意,配置shortcuts的activity必须要具备action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER!
所以清单文件应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.lagou.ktdemo.firstkotlin"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"></activity><activity android:name=".JavaActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><meta-dataandroid:name="android.app.shortcuts"android:resource="@xml/shortcuts" /></activity></application></manifest>
静态配置,基本就是这样。显然这样不够灵活,我们可以根据后台数据动态配置。
动态配置:Dynamic Shortcuts
private ShortcutManager mShortcutManager;private void setupShortcuts() {mShortcutManager = getSystemService(ShortcutManager.class);//获取已静态配置的列表List<ShortcutInfo> manifestShortcuts = mShortcutManager.getManifestShortcuts();int xmlSize = manifestShortcuts.size();//shortcut并非可以无限添加,先获取配置最大数int maxShortcutCount = mShortcutManager.getMaxShortcutCountPerActivity();int enableCount = maxShortcutCount - xmlSize;//待添加shortcutInfo列表List<ShortcutInfo> infoList = new ArrayList<>(enableCount);for (int i = 0; i < enableCount; i++) {Intent intent = new Intent(this, JavaActivity.class);intent.setAction(Intent.ACTION_VIEW);ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i).setShortLabel("简称" + i).setLongLabel("联系人名字很长:" + i).setIcon(Icon.createWithResource(this, R.drawable.amap_man)).setIntent(intent).build();infoList.add(info);}mShortcutManager.setDynamicShortcuts(infoList);
}
动态添加,这样基本就完成了,下面讲述如何动态删除?
shortcut除了长按时候可以列表形式展示在icon上方,也可以把单个item拖到桌面。类似这样
显然,动态删除,不但考虑删除列表,还要删除已放置桌面的快捷入口icon,但是文档提到只允许用户手动删除icon,那么我们删除与之对应的Activity后,用户再点击快捷入口icon会不会崩溃呢?显然系统提供了对策。
Pinning Shortcuts就是我们提到的桌面快捷入口icon。
动态删除:
private void removeShortcut(String id) {List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts();for (int i = 0; i < infos.size(); i++) {if (id.equals(infos.get(i).getId())) {mShortcutManager.disableShortcuts(Arrays.asList(id), "暂不可用");}}mShortcutManager.removeDynamicShortcuts(Arrays.asList(id));
}
- 首先我们先调用mShortcutManager.getPinnedShortcuts()来获取到所有的Pinning Shortcuts, 找到我们删除的那个,
- 然后通过disableShortcuts(List)来禁用掉该项,
- 最后我们还要用过removeDynamicShortcuts(List)来从shortcuts中移除。
动态更新 信息:
private void updateShortcut(int index) {Intent intent = new Intent(this, JavaActivity.class);intent.setAction(Intent.ACTION_VIEW);intent.putExtra("msg", "some info....");ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + index).setShortLabel("家").setLongLabel("地址很长的").setIcon(Icon.createWithResource(this, R.drawable.address)).setIntent(intent).build();mShortcutManager.updateShortcuts(Arrays.asList(info));
}
总结:
类似于 iOS 的 3D Touch,长按启动图标弹出几个快捷入口,入口最好不要超过 4 个,像搜索、扫描二维码、发帖等应用程序最常用功能的入口被称为静态 shortcut,不会随着用户不同或随着用户使用而改变。
还有一种像从某个存档点继续游戏、任务进度等与用户相关的上下文敏感入口被称为动态 shortcut,会因用户不同或随着用户使用不断变化。还有一种在 Android 8.0 (API level 26) 及以上系统版本上像固定网页标签等用户主动固定到桌面的快捷方式被称为固定 shortcut静态 shortcut 系统可以自动备份和恢复,动态 shortcut 需要应用自己备份和恢复,固定 shortcut 的图标系统无法备份和恢复因此需要应用自己完成
android:shortcutId 和 android:shortcutShortLabel 属性是必须的,android:shortcutShortLabel 不能超过 10 个字符,android:shortcutLongLabel 不能超过 25 个字符,android:icon 不能包含 tint
获取 ShortcutManager 的方式有两个: getSystemService(ShortcutManager.class) 和 getSystemService(Context.SHORTCUT_SERVICE)
官网的文档: https://developer.android.com/preview/shortcuts.html
Android 长按3Dtouch快捷方式相关推荐
- android 4.2 桌面快捷方式,Android 添加桌面快捷方式操做
/** android * 为程序建立桌面快捷方式 app */ ide private void addShortcut(){ this Intent shortcut = new Intent(& ...
- android判断和创建快捷方式(4.03测试通过)
android判断和创建快捷方式(4.03测试通过) 整理了网上的创建方式的代码,对于快捷方式的判断使用系统api获取当前启动器来处理,这样系统定制过或者启动器不一样也没关系 . 一.加权限和声明目标 ...
- android长截屏代码,android长截屏原理及实现代码
android长截屏原理及实现代码 发布时间:2020-08-31 06:55:16 来源:脚本之家 阅读:158 作者:Android笔记 小米系统自带的长截屏应该很多人都用过,效果不错.当长截屏时 ...
- android view存储为jpg,Android长按imageview把图片保存到本地的实例代码
工具类 之前用 AsyncTask 现在改用rxJava public class SaveImageUtils { public static void imageSave(final ImageV ...
- android 应用创建桌面快捷方式
android的快捷方式比较简单,就是发一个系统的广播,然后为快捷方式设置Intent--- package com.xikang.android.slimcoach.utils; /** * @au ...
- Android长截图与长图分享
1.第一步如何实现长截图 Android长截屏– ScrollView,ListView及RecyclerView截屏 这个链接地址写的很清楚了,建议进行实际操作. 概述:其实长截图就是将布局存成图像 ...
- android 多个shortCut快捷方式实现以及对58同城快捷方式的实现思路的研究
这几天,项目中有个新需求,需要按照模块添加不同的快捷方式到桌面上,从而方便用户的使用.特意进行了研究并分析了下58上面桌面快捷方式的实现. 首先多个shortcut的实现: <activitya ...
- android 照片拼接长图_最智能的 Android 长图拼接应用:图片自动连接
点击「添加」图标,按拼接顺序勾选图片(免费版上限为 5 张),倘若不小心弄错了顺序,无需清除重新添加,可以通过按住图片拖动来进行排列.一切准备妥当之后,下一步就可以点击「连接!」来生成长图了. 生成的 ...
- android 界面长按,Android 主界面长按创建快捷方式
Android中创建快捷方式主要有两种方式.一是在代码中直接加入生成桌面快捷方式的代码:二是通过小部件加入; 这篇文章主要讲另外一种方法! 1.通过在AndroidManifest文件里为Activi ...
- Android 长按APP图标弹出快捷方式(shortcuts)
今天给大家分享一份关于Android 7.1版本的一个新特性 Shortcut 它是谷歌在Api Level 25提出来的 类似苹果3D touch 但是没有压力感应.在安卓中完全就是长按 某些APP ...
最新文章
- JAVA 笔记no.2
- deepin启动盘制作工具_balenaEtcher for mac(启动盘制作工具) v1.5.70已更新
- docker 私有仓库与Harbor
- php 操作 mysql 数据库常用方法集合
- 配置syslog发送_Citrix ADC Syslog配置推荐
- c#访问各数据库的providerName各驱动
- C++11新特性之auto关键字的使用
- 计算机组装与配置答案,解答电脑组装配置有哪些
- php fakepath,javascript - chrome upload file 出现 fakepath,请解决方案
- Android NDK开发1——开发流程+依赖外部so+生成自实现so+静态注册JNI+动态注册JNI
- 计算机远程操作之后怎么保护,电脑远程控制怎么操作 两种方法介绍
- 【html5期末大作业】基于HTML仿QQ音乐官网网站
- 一个low逼的boofuzz脚本生成器
- Android Studio实现简单计算器
- 小米手机不为人知的秘密—后台静默安装任何应用
- 为什么刹车热了会失灵_急!我的刹车为什么突然不管用了?
- [CPNet]-理想亲和图的生成以及作用——Blank
- 解决office2003 32位卸载不干净 无法安装office2013 64位在问题
- 剑指offer答案 c语言,剑指offer 题目整理
- 阿里云服务器上搭建Discuz论坛