赞
踩
简介
帮助用户快速启动应用程序中的常见或推荐功能
创建方式
静态快捷方式:在打包到APK或应用包中的资源文件中定义。适合在用户与应用程序互动的整个生命周期内使用一致结构链接到内容的应用程序,即固定功能,固定跳转的页面。
动态快捷方式:只能在运行时由应用发布,更新和删除(静态和动态加在一起最多四个,因为大多数启动器只能显示四个)。用于上下文相关的应用程序中的操作,快捷方式将需要经常更新。
固定快捷方式:如果用户授予许可,则可以在运行时将固定的快捷方式添加到受支持的启动器中(无数量限制)。该方式生成的快捷方式内容一般由用户驱动,比如浏览器生成特定网页的快捷方式、遥控器生成特定设备的快捷方式。
使用
静态快捷方式
在res下新建xml文件夹,然后新建文件作为静态快捷方式配置文件,这边新建的文件名叫shortcuts.xml,填写快捷方式相关配置
android:shortcutId="play"
android:shortcutShortLabel="@string/play_shortcut_short_label"
android:shortcutLongLabel="@string/play_shortcut_long_label"
android:enabled="true"
android:shortcutDisabledMessage="@string/play_disabled_message"
android:icon="@drawable/video">
android:action="android.intent.action.VIEW"
android:targetPackage="com.dean.smartApp"
android:targetClass="com.dean.smartApp.MainActivity">
android:name="shortcut"
android:value="play"/>
android:shortcutId="music"
android:enabled="true"
android:icon="@drawable/music"
android:shortcutShortLabel="@string/music_shortcut_short_label"
android:shortcutLongLabel="@string/music_shortcut_long_label"
android:shortcutDisabledMessage="@string/music_disabled_message">
android:action="android.intent.action.VIEW"
android:targetPackage="com.dean.smartApp"
android:targetClass="com.dean.smartApp.MainActivity">
android:name="shortcut"
android:value="music"/>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354在Manifest启动Activity下添加配置文件
android:resource="@xml/shortcuts" /> 123456789
动态快捷方式
使用该方式可以引导用户自定义快捷方式以快速打开某个页面
新建快捷方式,这边方法和上面xml中差不多
ShortcutInfo shortcut = new ShortcutInfo.Builder(context, "play")
.setShortLabel("高清影视")
.setLongLabel("16K高清,不一样的体验")
.setIcon(Icon.createWithResource(context, R.drawable.icon_shortcut_play))
.setIntent(playIntent)
.build();123456
更新快捷方式列表
//通过SystemService获取Shortcut管理类ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);//通过setDynamicShortcuts替换原有整个快捷方式列表shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));//也可以通过addDynamicShortcuts来增加一些快捷方式shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));//也可以通过updateShortcuts来更新原有的快捷方式列表shortcutManager.updateShortcuts(Arrays.asList(shortcut));//移除所有快捷方式shortcutManager.removeAllDynamicShortcuts();//通过ID删除指定的快捷方式shortcutManager.removeDynamicShortcuts(shortcutIds);123456789101112
固定快捷方式
Android 8.0(API级别26)及更高版本上支持
第一种:之前静态和动态创建的快捷方式,在桌面长按应用图标会显示快捷方式列表,这时长按列表某一项然后拖动到桌面空白位置即可。
第二种:代码创建
//获取ShortcutManagerShortcutManager shortcutManager =
context.getSystemService(ShortcutManager.class);//通过isRequestPinShortcutSupported来判断当前设备是否支持固定快捷方式if (shortcutManager.isRequestPinShortcutSupported()) {
//拿到需要固定的快捷方式,可以是之前静态或动态创建好的
ShortcutInfo pinShortcutInfo =
new ShortcutInfo.Builder(context, "play").build();
//创建一个意图
Intent pinnedShortcutCallbackIntent =
shortcutManager.createShortcutResultIntent(pinShortcutInfo);
//和其他系统控件交互一样需要延时意图PendingIntent
PendingIntent successCallback = PendingIntent.getBroadcast(context,0,pinnedShortcutCallbackIntent,0);
//创建固定快捷方式
shortcutManager.requestPinShortcut(pinShortcutInfo,successCallback.getIntentSender());}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。