Android 8.0 应用快捷方式(ShortcutManager)的使用
来源:互联网 发布:别了司徒雷登 知乎 编辑:程序博客网 时间:2024/06/16 18:41
在Android 7.1(API 25)之后添加的新功能,应用快捷方式。ShortcutManager管理一个应用程序的快捷方式。只要长按APP图标支持快捷方式,通过快捷键,用户可以快速访问任意一个Activity。
简单地理解:在长按应用图标的情况下,在应用图标上显示的快捷方式,该快捷方式可以点击进入Activity,长按拖动创建一个在Launcher上的图标。
现在市场上已经是有很多应用增加了这项功能,如:支付宝、知乎、印象笔记、美团等,大概的效果如下图:
快捷方式的特点
静态和动态快捷方式
用户执行特定手势时,支持的手机中会显示静态快捷方式和动态快捷方式。 在7.1、8.0系统中,大部分手势是对应用程序图标的长按,但实际手势可能与其他手机应用程序不同。
重要的安全注意事项:所有的快捷方式信息都存储在凭证加密存储中,因此您的应用只有在解锁设备后才能访问用户的快捷方式。
静态方式
静态方式就是通过xml文件进行配置,就像ActionBar的选项一样。
先配置快捷方式的选项:
res/xml/static_shortcuts.xml 主要配置如下参数:id、图标、标题、意图,还有分类,是否可用。
<?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_bnsports" android:shortcutDisabledMessage="@string/lable_shortcut_static_search_disable" android:shortcutId="shortcut_id_search" android:shortcutLongLabel="@string/lable_shortcut_static_search_long" android:shortcutShortLabel="@string/lable_shortcut_static_search_short"> <intent android:action="android.intent.action.VIEW" android:targetClass="com.example.song.jackwaiting.MainActivity" android:targetPackage="com.example.song.jackwaiting" /> <!--当前只有这个分类--> <categories android:name="android.shortcut.conversation" /> </shortcut> </shortcuts>
配置完图标的之后,再打开AndroidManiFest.xml配置启动的Activity
AndroidManiFest.xml
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!--add static shortcut--> <meta-data android:name="android.app.shortcuts" android:resource="@xml/static_shortcut" /> </activity>
动态方式
那么动态方式,是通过ShortcutManager实现快捷方式的增加、删除、更新的操作,使用起来很简单。下面就放一下简单的配置代码。
/** * 先生成一个一个的快捷方式的对象 * 对象需要配置:图标、标题、意图,其他的配置具体可以到官网查看 */ if (Build.VERSION.SDK_INT >= 25) { ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, SHORTCUT_ID_SEARCH) .setShortLabelResId(R.string.lable_shortcut_static_search_long) .setLongLabelResId(R.string.lable_shortcut_static_search_long) .setIcon(Icon.createWithResource(this, R.drawable.ic_bnsports)) .setIntent(new Intent(this, MainActivity.class)) .build(); ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); //这样就可以通过长按图标显示出快捷方式了 shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo)); }
配置好之后看一下效果:
常用的方法:
移除快捷方式
void removeDynamicShortcuts(@NonNull List shortcutIds);
移除快捷方式,并且拖到launcher上的图标会变灰,这个方法底层最终调用的方法跟removeDynamicShortcuts一样
void disableShortcuts(@NonNull List shortcutIds);
使拖到launcher的图标可用。 静态添加的快捷方式不能使用该方法,使用会报错
void enableShortcuts(@NonNull List shortcutIds);
更新快捷方式,例如图标、标题
boolean updateShortcuts(List shortcutInfoList);
显示的顺序问题:添加在前的显示在下方,后面添加的显示在上面。
这个类的使用还是比较简单的。添加了这项功能的APP,长按的效果跟iOS的3D touch差不多的视觉体验。
LauncherApps类为启动程序应用程序提供访问快捷方式的API。
最后附上官网地址:
https://developer.android.google.cn/reference/android/content/pm/ShortcutManager.html
- Android 8.0 应用快捷方式(ShortcutManager)的使用
- Android 7.1 应用快捷方式(ShortcutManager的使用)
- Android8.0全面了解ShortcutManager,并创建应用快捷方式(适用于8.0及8.0以下系统)
- android 快捷方式的使用
- Android Studio 快捷方式的使用
- Android中应用的快捷方式的创建
- Android Launcher桌面应用快捷方式的开发
- Android Launcher桌面应用快捷方式的开发
- Android Launcher桌面应用快捷方式的开发
- Android Launcher-----桌面应用快捷方式的开发!
- android应用创建快捷方式
- android应用添加快捷方式
- android应用添加快捷方式
- Android应用桌面快捷方式
- Android应用快捷方式
- 【Tech-Android-Other】Android创建应用的桌面的快捷方式
- 创建应用的快捷方式
- Android 添加删除应用快捷方式
- Sublime Text 3
- idea的安装
- maven与eclipse的整合
- 选择器竟然有这么多!!!w(゚Д゚)w
- git中手动删除的文件如何在git中删除
- Android 8.0 应用快捷方式(ShortcutManager)的使用
- JAVA面试之集合框架(三)
- vc6版本工程在vs2013版本打开需要安装vs多字节补丁
- JAVA 打印菱形
- 学习历程—程序设计思维实例
- logback的使用和logback.xml详解
- java基础-数组转换,删除,去重复(自己玩儿)
- 在Ubuntu上打开SSH服务
- mtk lcd调试