Android - 应用快捷方式ShortCut

本文详细介绍了Android应用快捷方式的创建和使用,包括静态、动态和固定快捷方式。静态快捷方式在资源文件中定义,适用于固定功能;动态快捷方式在运行时发布,适合上下文相关操作;固定快捷方式则允许用户自定义,适用于Android 8.0及以上版本。通过设置XML配置文件和编程方式,开发者可以实现快捷方式的创建、更新和用户自定义功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

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

创建方式

  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">
	<!--用户点击该快捷方式发生的意图-->
    <intent
      android:action="android.intent.action.VIEW"
      <!--注意:这边是应用包名,写错会无法打开指定页面-->
      android:targetPackage="com.dean.smartApp"
      android:targetClass="com.dean.smartApp.MainActivity">
	  <!--通过intent意图里面配置extra来告诉MainActivity需要展示的fragment-->
	  <extra
			android:name="shortcut"
			android:value="play"/>
	</intent>
	<!--为应用程序的快捷方式执行的操作类型提供分组,例如创建新的聊天消息-->
    <categories android:name="android.shortcut.conversation" />
  </shortcut>
  
  <shortcut
    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">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.dean.smartApp"
      android:targetClass="com.dean.smartApp.MainActivity">
	  <extra
			android: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);
	//和其他系统控件交互一样需要延时意图PendingIntent
    PendingIntent successCallback = PendingIntent.getBroadcast(context,0,pinnedShortcutCallbackIntent,0);
	//创建固定快捷方式
    shortcutManager.requestPinShortcut(pinShortcutInfo,successCallback.getIntentSender());
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值