Android Shortcuts是一种功能,它允许用户在长按应用图标时快速访问应用内的特定功能,而无需先打开应用。这一功能为用户提供了更便捷的操作方式,提升了应用的使用体验。以下是关于Android Shortcuts的详细介绍:
一、基本概念
- 定义:Android Shortcuts是在图标位置长按app图标出现的快捷方式,通过这一功能可以快速访问应用中的某些功能。
- 类型:Android Shortcuts分为静态快捷方式和动态快捷方式两种。
二、类型介绍
-
静态快捷方式
- 定义:静态快捷方式是在应用的APK或APK Bundle中直接定义的,安装完应用后就会存在快捷方式入口。
- 特点:适用于那些在整个生命周期中intent不会改变,始终完成同一种行为的功能,如联系人、相机等。
- 实现方式:在res/xml目录下创建shortcuts.xml文件,并在AndroidManifest.xml中通过<meta-data>标签引用该XML文件。
1、enabled:表示当前快捷方式是否可使用 2、 icon: 快捷方式图标 3、 shortcutDisabledMessage: 快捷方式不可使用时显示的名字 4、 shortcutId:快捷方式标识 5、 shortcutLongLabel:长按下图标弹出来列表框中每个快捷名 6、 shortcutShortLabel:快捷是可以单独显示在桌面上的,显示名为shortcutShortLabel 7、 targetClass:点击快捷方式进入的Activity 8、categories 默认写死即可 注意,shortcutLongLabel和shortcutShortLabel,不可以直接引用文字,不然会报错.!!!谁加谁知道.经过以上的 步骤之后,就可以看到最开始的效果图了!.
-
动态快捷方式
- 定义:动态快捷方式是在应用运行时通过ShortcutManager API创建的,可以随时更新、添加和删除。
- 特点:适用于那些需要根据用户行为或偏好动态调整快捷方式的应用场景。
- 实现方式:通过调用ShortcutManager的setDynamicShortcuts方法添加或更新快捷方式。
设置或者新增 setDynamicShortcuts; addDynamicShortcuts; 设置桌面快捷 requestPinShortcut; 修改 updateShortcuts; 删除 removeDynamicShortcuts; removeAllDynamicShortcuts; @RequiresApi(api = Build.VERSION_CODES.O) private void getNewShortcutInfo() { // 系统提供的 ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class); // 按下返回按钮跳转的activity Intent intent1 = new Intent(this, MainActivity.class); intent1.setAction(Intent.ACTION_VIEW); // 目标activity Intent intent2 = new Intent(this, PublishPostActivity.class); intent2.setAction("com.shark.xxx.BACK"); Intent[] intents = new Intent[2]; intents[0] = intent1; intents[1] = intent2; // 一个具体的快捷对象 ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1") // id 标识是唯一的 .setShortLabel("Web site") // 桌面快捷方式显示的名字 .setLongLabel("第一个") // 长按弹窗显示的名字 .setIcon(Icon.createWithResource(this, R.mipmap.acc_circle)) // 显示的Icon .setIntents(intents) // 意图设置 .build(); // 构建 ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "id2") .setShortLabel("Web site") .setLongLabel("第二个") .setIcon(Icon.createWithResource(this, R.mipmap.acc_circle)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.csdn.com/"))) .build(); ShortcutInfo shortcut3 = new ShortcutInfo.Builder(this, "id3") .setShortLabel("Web site") .setLongLabel("第三个") .setIcon(Icon.createWithResource(this, R.mipmap.acc_circle)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.github.com/"))) .build(); // 创建长按快捷键 mShortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2, shortcut3)); // 创建桌面快捷键 PendingIntent successCallback = PendingIntent.getBroadcast(this, 0, mShortcutManager.createShortcutResultIntent(shortcut), 0); mShortcutManager.requestPinShortcut(shortcut,successCallback.getIntentSender()); }
三、使用场景
- 快速访问常用功能:如拍照、发送短信、查看最新消息等。
- 提升用户体验:通过减少操作步骤,提高用户使用应用的效率和满意度。
四、注意事项
- 数量限制:每个应用最多可以创建五个快捷方式(包括静态和动态),但大多数设备上只能展示四个。
- 设备兼容性:Shortcuts功能是从Android 7.1(API 25)开始引入的,因此只能在支持该API版本的设备上使用。同时,还需要Launcher的支持。
- 国际化:在定义快捷方式时,需要注意对快捷方式的名称和描述进行国际化处理,以适应不同地区的用户需求。
五、示例代码
<!-- res/xml/shortcuts.xml -->
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="camera"
android:enabled="true"
android:icon="@drawable/ic_camera"
android:shortcutShortLabel="@string/camera_short_label"
android:shortcutLongLabel="@string/camera_long_label">
<intent
android:action="android.media.action.IMAGE_CAPTURE"
android:targetPackage="com.example.app"
android:targetClass="com.example.app.CameraActivity" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
<!-- AndroidManifest.xml -->
<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>