Android 7.1 应用快捷方式(ShortcutManager的使用)
来源:互联网 发布:培训矩阵图 编辑:程序博客网 时间:2024/06/05 20:09
在Android 7.1(API 25)之后添加的新功能,这里就暂且翻译成:快捷方式,简单地理解:在长按应用图标的情况下,在应用图标上显示的快捷方式,该快捷方式可以点击进入Activity,长按拖动创建一个在Launcher上的图标。
现在市场上已经是有很多应用增加了这项功能,如:印象笔记、支付宝、哔哩哔哩、IT之家、知乎、美团,以上是我的手机安装的应用中看到的,其他的就暂时没了解到,可以看一下效果。
实现有两种方式
- 静态方式,通过在xml中配置快捷方式,配置名称、图标、意图这些必须的参数,最后在应用启动的Activity配置一下就可以了。
- 动态方式,通过Android7.1新添加的ShortcutManager进行管理,可以对快捷方式进行添加、删除、更新,也可以修改上面的静态方式显示的快捷方式。
静态方式
静态方式就是通过xml文件进行配置,就像ActionBar的选项一样。
先配置快捷方式的选项:
res/xml/static_shortcuts.xml 下面配置的参数也是比较清晰的,有:id、图标、标题、意图,还有分类,是否可用。
<!--离线视频--><shortcut android:enabled="true" android:icon="@drawable/ic_shortcut_download" android:shortcutDisabledMessage="@string/lable_shortcut_static_download_disable" android:shortcutId="shortcut_id_download" android:shortcutLongLabel="@string/lable_shortcut_static_download_long" android:shortcutShortLabel="@string/lable_shortcut_static_download_short"> <intent android:action="android.intent.action.VIEW" android:targetClass="com.young.demo.launchershortcut.StaticShortcutDownload" android:targetPackage="com.young.demo.launchershortcut" /> <!--当前只有这个分类--> <categories android:name="android.shortcut.conversation" /></shortcut>
配置完图标的之后,再打开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>
那么静态方式就配置好了,上面快捷方式配置的Activity的就不贴上来了,就是一个简单的Activity而已。
看一下效果图:
动态方式
那么动态方式,是通过ShortcutManager实现快捷方式的增加、删除、更新的操作,google的大神都配置好了方法,使用起来很简单。下面就放一下简单的配置代码。
/** * 先生成一个一个的快捷方式的对象 * 对象需要配置:图标、标题、意图,其他的配置具体可以到官网查看 */ShortcutInfo likeShortcut = new ShortcutInfo.Builder(this, SHORTCUT_ID_LIKE) .setShortLabelResId(R.string.lable_shortcut_dynamic_like_short) .setLongLabelResId(R.string.lable_shortcut_dynamic_like_long) .setIcon(Icon.createWithResource(this, R.drawable.ic_shortcut_like)) .setIntent(new Intent(this, DynamicShortcutLike.class)) .build();ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);//这样就可以通过长按图标显示出快捷方式了shortcutManager.setDynamicShortcuts(Arrays.asList(likeShortcut));
上面两个是通过动态方式添加的。
移除快捷方式
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差不多的视觉体验。
这是完整代码地址:https://github.com/daynearby/LauncherShortcut
最后附上官网地址:
https://developer.android.google.cn/reference/android/content/pm/ShortcutManager.html
- Android 7.1 应用快捷方式(ShortcutManager的使用)
- Android 8.0 应用快捷方式(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 添加删除应用快捷方式
- ubuntu 的基本命令
- GUI1
- 常用数学符号的 LaTeX 表示方法
- UVa 712 S-Trees
- mysql explain 的type解释
- Android 7.1 应用快捷方式(ShortcutManager的使用)
- SpringMVC实现查询结果下载为CSV文件
- [python]leetcode(105). Construct Binary Tree from Preorder and Inorder Traversal
- 直接插入、冒泡、快速、简单选择、堆、归并排序算法
- JVM内存区域和内存溢出异常
- 3301: [USACO2011 Feb] Cow Line 康托展开
- HTML5拖放
- 属性动画的几种实现方式
- hdu 5428(质因数分解)