当前位置: 代码迷 >> 综合 >> Android - 快捷方式
  详细解决方案

Android - 快捷方式

热度:12   发布时间:2024-03-07 15:17:04.0

简介

帮助用户快速启动应用程序中的常见或推荐功能

创建方式

  1. 静态快捷方式:在打包到APK或应用包中的资源文件中定义。适合在用户与应用程序互动的整个生命周期内使用一致结构链接到内容的应用程序,即固定功能,固定跳转的页面。
  2. 动态快捷方式:只能在运行时由应用发布,更新和删除(静态和动态加在一起最多四个,因为大多数启动器只能显示四个)。用于上下文相关的应用程序中的操作,快捷方式将需要经常更新。
  3. 固定快捷方式:如果用户授予许可,则可以在运行时将固定的快捷方式添加到受支持的启动器中(无数量限制)。该方式生成的快捷方式内容一般由用户驱动,比如浏览器生成特定网页的快捷方式、遥控器生成特定设备的快捷方式。

使用

  • 静态快捷方式
  1. 在res下新建xml文件夹,然后新建文件作为静态快捷方式配置文件,这边新建的文件名叫shortcuts.xml,填写快捷方式相关配置
<?xml version="1.0" encoding="utf-8"?>
<!--根标签是shortcuts代表一堆快捷方式-->
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><!--每个shortcut标签代表一个快捷方式--><shortcut<!--必须值:ID值,后面动态时可通过这个ID控制该快捷方式的显示或隐藏,不可以使用String资源文件里面的值引入-->android:shortcutId="play"<!--必须值:显示快捷方式后的简短描述,长度不超过10个字符-->android:shortcutShortLabel="@string/play_shortcut_short_label"<!--可选值:扩展短语,有足够空间才展示,长度不超过25个字符-->android:shortcutLongLabel="@string/play_shortcut_long_label"<!--可选值:默认true,如果设置为false,用户点击该快捷方式时无效,这时最好配置shortcutDisabledMessage告诉用户为什么无效-->android:enabled="true"<!--可选值:当该快捷方式无效时提示文字,enabled为true不起作用--> android:shortcutDisabledMessage="@string/play_disabled_message"<!--可选值:快捷方式的图标-->android:icon="@drawable/video"><!--用户点击该快捷方式发生的意图--><intentandroid:action="android.intent.action.VIEW"android:targetPackage="com.dean.smartApp"android:targetClass="com.dean.smartApp.MainActivity"><!--通过intent意图里面配置extra来告诉MainActivity需要展示的fragment--><extraandroid:name="shortcut"android:value="play"/></intent><!--为应用程序的快捷方式执行的操作类型提供分组,例如创建新的聊天消息--><categories android:name="android.shortcut.conversation" /></shortcut><shortcutandroid: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"><intentandroid:action="android.intent.action.VIEW"android:targetPackage="com.dean.smartApp"android:targetClass="com.dean.smartApp.MainActivity"><extraandroid:name="shortcut"android:value="music"/></intent><categories android:name="android.shortcut.conversation" /></shortcut>
</shortcuts>
  1. 在Manifest启动Activity下添加配置文件
<activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><meta-data android:name="android.app.shortcuts"android:resource="@xml/shortcuts" /> 
</activity>
  • 动态快捷方式
    使用该方式可以引导用户自定义快捷方式以快速打开某个页面
  1. 新建快捷方式,这边方法和上面xml中差不多
ShortcutInfo shortcut = new ShortcutInfo.Builder(context, "play").setShortLabel("高清影视").setLongLabel("16K高清,不一样的体验").setIcon(Icon.createWithResource(context, R.drawable.icon_shortcut_play)).setIntent(playIntent).build();
  1. 更新快捷方式列表
//通过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);
  • 固定快捷方式
    Android 8.0(API级别26)及更高版本上支持
  1. 第一种:之前静态和动态创建的快捷方式,在桌面长按应用图标会显示快捷方式列表,这时长按列表某一项然后拖动到桌面空白位置即可。
  2. 第二种:代码创建
//获取ShortcutManager
ShortcutManager shortcutManager =context.getSystemService(ShortcutManager.class);
//通过isRequestPinShortcutSupported来判断当前设备是否支持固定快捷方式
if (shortcutManager.isRequestPinShortcutSupported()) {
    //拿到需要固定的快捷方式,可以是之前静态或动态创建好的ShortcutInfo pinShortcutInfo =new ShortcutInfo.Builder(context, "play").build();//创建一个意图Intent pinnedShortcutCallbackIntent =shortcutManager.createShortcutResultIntent(pinShortcutInfo);//和其他系统控件交互一样需要延时意图PendingIntentPendingIntent successCallback = PendingIntent.getBroadcast(context,0,pinnedShortcutCallbackIntent,0);//创建固定快捷方式shortcutManager.requestPinShortcut(pinShortcutInfo,successCallback.getIntentSender());
}
  相关解决方案