AndroidShortcuts使用详解
来源:互联网 发布:node pm2 静态目录 编辑:程序博客网 时间:2024/05/21 17:47
7.1的时候出现了一个长按图标出现的功能列表的效果,类似于2015年ios的3dTouch功能.奈何国产,大家懂得,到目前用了一加五才可以感受这个功能的好处.
效果图来了
shortcuts的介绍
其中App Shortcuts是指在桌面长按app图标而出现的快捷方式, 可以为你的app的关键功能添加更快速的入口而不用先打开app,点击快捷方式可以访问应用功能, 并且这种快捷方式也可以被拖拽到桌面单独放置, 变成单独的桌面快捷方式.
有两种shortcuts:
- 静态的: 在xml中定义, 适用于一些通用的动作.
- 动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.
数量
每一个应用目前最多可以有5个shortcuts(静态 + 动态)
运行条件
应用添加App Shortcuts是Android 7.1(API 25)的API, 所以只能在Android 7.1的设备上显示, 同时需要launcher支持, 比如Pixel launcher(Pixel设备的默认launcher), Now launcher(Nexus设备上的launcher)现在就支持, 其他launcher也可以提供支持.
静态的shortcuts的使用
清单文件配置
<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 version="1.0" encoding="utf-8"?><shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:icon="@drawable/add" android:shortcutId="add_website" android:shortcutLongLabel="@string/add_new_website" android:shortcutShortLabel="@string/add_new_website_short"> <!--最左侧或者添加桌面后显示的图标--> <!--id标识--> <!--长按显示的文字--> <!--添加到桌面显示的文字--> <intent android:action="cn.yky.test.ADD_WEBSITE" android:targetClass="cn.yky.test.MainActivity" android:targetPackage="cn.yky.test" /> <!--第一个是意图标识--> <!--第二个参数是希望打开的界面--> <!--希望打开界面的包名--> </shortcut></shortcuts>
注意,shortcutLongLabel和shortcutShortLabel,不可以直接引用文字,不然会报错.!!!谁加谁知道.经过以上的步骤之后,就可以看到最开始的效果图了!.
动态添加
动态的shortcuts可以在用户使用app的过程中构建, 更新, 或者删除.
ShortcutManager
- 设置或者新增 setDynamicShortcuts; addDynamicShortcuts;
- 修改 updateShortcuts;
- 删除 removeDynamicShortcuts; removeAllDynamicShortcuts;
动态添加三个
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { mShortcutManager = getSystemService(ShortcutManager.class); getNewShortcutInfo(); } /** * 动态添加三个 */ @RequiresApi(api = Build.VERSION_CODES.N_MR1) private void getNewShortcutInfo() { ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1") .setShortLabel("Web site") .setLongLabel("第一个") .setIcon(Icon.createWithResource(this, R.drawable.link)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"))) .build(); ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "id2") .setShortLabel("Web site") .setLongLabel("第二个") .setIcon(Icon.createWithResource(this, R.drawable.link)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.csdn.com/"))) .build(); ShortcutInfo shortcut3 = new ShortcutInfo.Builder(this, "id3") .setShortLabel("Web site") .setLongLabel("第三个") .setIcon(Icon.createWithResource(this, R.drawable.link)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.github.com/"))) .build(); mShortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2, shortcut3)); }
效果图
依次类推,就可以动态增删改我们的快捷方式了.虽然你想用,但是国产手机估计还得一年以内的时间能普及到吧.
联系方式
本人技术有限,还有很多不完美的地方,欢迎指出.(写作不易,谢谢您的star支持)
- QQ:152046273
- Email:yukuoyuan@hotmail.com
- CSDN博客地址
- Github博客地址
- Github地址
阅读全文
1 0
- AndroidShortcuts使用详解
- AndroidShortcuts
- pdf详解 iTextSharp 使用详解
- 【Git使用详解】EGit使用详解
- POI使用详解 Apache POI使用详解
- function对象使用使用详解
- CheckStyle使用详解
- VSS使用详解释
- JDom使用详解
- JCoverage使用详解
- 正则表达式使用详解
- showModalDialog(),showModelessDialog()使用详解
- Ghost 使用详解
- SSI使用详解
- GDB使用详解
- SSI使用详解
- SSI使用详解1
- 正则表达式使用详解
- IDEA spark开发过程中出现cannot resolve symble xx 问题解决方法汇总
- ES6
- Ubuntu14.04下本地仓库项目上传至github远程仓库
- ServiceManager.java
- YOLOV2+window10+cuda8.0+cudnn6.0安装过程
- AndroidShortcuts使用详解
- E-MapReduce上Hive创建基于oss的外部表
- win10x解决安装pywin32-221.win32-py3.5.exe时报错没有发现注册表的方法
- python 拆分日志文件
- 13123
- 解决程序包和运行环境包的一种依赖
- ubuntu 删除anaconda
- 用队列ConcurrentLinkedQueue模拟生产者和消费者
- Rxjava学习记录(一)