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

Android 快捷方式 shortcuts 使用

热度:77   发布时间:2024-03-07 05:13:06.0
先上两个图

111
222

官方说明

快捷方式可帮助用户快速访问应用程序的各个部分,从而为用户提供特定类型的内容。
使用快捷方式交付内容的方式取决于您的用例以及快捷方式的上下文是应用程序驱动还是用户驱动。尽管静态快捷方式的上下文不会更改,而动态快捷方式的上下文会不断更改,但是两种情况下的上下文都是由您的应用程序驱动的。如果用户选择自己希望您的应用向其交付内容的方式(例如固定快捷方式),则上下文由用户定义。

有三种方式

1.静态快捷方式

最适合在用户与应用程序互动的整个生命周期内使用一致结构链接到内容的应用程序。由于大多数启动器一次 只能显示四个快捷方式,因此静态快捷方式对于常见活动很有用。

1.1 在清单文件中添加meda-data
    <applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><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><activity android:name=".ShortcutActivity"/></application>
1.2 创建shortcuts资源文件,res/xml/shortcuts.xml

一个shortcut标签表示一个快捷方式,这里我创建了两个,但是id不同其他都是相同的。
必须提供一个android:shortcutId和 android:shortcutShortLabel。所有其他值都是可选的。
各个属性定义:
shortcutId:id
shortcutShortLabel:简短的短语,描述了快捷方式的用途。尽可能将快捷方式的“简短描述”的长度限制为10个字符。
shortcutLongLabel:描述快捷方式用途的扩展短语。如果有足够的空间,启动器将显示该值而不是shortcutShortLabel。如果可能,将快捷方式的“详细描述”的长度限制为25个字符。
shortcutDisabledMessage:当用户尝试启动禁用的快捷方式时,在受支持的启动器中显示的消息。该消息应向用户说明为什么现在禁用了快捷方式。如果android:enabled为,则此属性的值无效true。
enabled:默认值是true,如果将其设置为false,还应该设置一个android:shortcutDisabledMessage,以说明为什么禁用了快捷方式。如果您不需要提供此类消息,则最简单的方法是从XML文件中完全删除快捷方式。
icon:图标,也可以是包含图像的资源文件。

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcutandroid:shortcutId="compose"android:icon="@mipmap/ic_launcher"android:shortcutShortLabel="@string/addShortcuts"android:shortcutLongLabel="@string/addNewWebsite"android:shortcutDisabledMessage="@string/shortcutsMessage"><intentandroid:action="android.intent.action.VIEW"android:targetClass="com.shy.shortcutstest.ShortcutActivity"android:targetPackage="com.shy.shortcutstest" /></shortcut><shortcutandroid:shortcutId="compose1"android:icon="@mipmap/ic_launcher"android:shortcutShortLabel="@string/addShortcuts"android:shortcutLongLabel="@string/addNewWebsite"android:shortcutDisabledMessage="@string/shortcutsMessage"><intentandroid:action="android.intent.action.VIEW"android:targetClass="com.shy.shortcutstest.ShortcutActivity"android:targetPackage="com.shy.shortcutstest" /></shortcut>
</shortcuts>

附上资源文件

<resources><string name="addShortcuts">Shortcuts</string><string name="addNewWebsite">AddNewWebsite</string><string name="shortcutsMessage">shortcutsMessage</string></resources>
创建好点击快捷方式的activity,也就是shortcut中intent,targetClass和targetOackage的指向。此时就可以跳转到制定的activity了。

2.动态快捷方式

动态快捷方式提供了指向应用程序中特定于上下文的特定操作的链接。这些操作可能会在您的应用使用之间发生变化,甚至在您的应用运行时也会发生变化。
在某个特定的时期为某个页面,或某个动作,甚至可以参与到周期较长的业务逻辑当中,添加一个快捷方式,以快速到达当前状态。

2.1 获得ShortcutManager
mSm = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
2.2 调用ShortcutManager的api执行添加、设置、删除快捷方式的操作

setDynamicShortcuts():重新定义动态快捷键的完整列表。
addDynamicShortcuts():以增加动态快捷键的现有列表。
updateShortcuts():更新。
removeDynamicShortcuts():删除一组。
removeAllDynamicShortcuts():删除全部。
传入参数都是list,list中实际都是ShortcutInfo对象。

添加
Intent intent = new Intent(this, ShortcutActivity2.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("name","孙三");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
    ShortcutInfo builder = new ShortcutInfo.Builder(this, "id").setShortLabel("动态标签").setLongLabel("长长动态标签").setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(intent).build();mSm.addDynamicShortcuts(Arrays.asList(builder));
}
删除
List<String> id = new ArrayList<>();
id.add("id");
mSm.removeDynamicShortcuts(id);

ShortcutManager中还提供了获得快捷方式的函数,

mSm.getDynamicShortcuts();  //获得动态的快捷方式
mSm.getPinnedShortcuts();   //获得固定的快捷方式
mSm.getManifestShortcuts(); //获得静态的快捷方式

3. 固定快捷方式

在Android 8.0(API级别26)及更高版本上,您可以创建固定的快捷方式。与静态和动态快捷方式不同,固定的快捷方式在支持的启动器中显示为单独的图标。图1显示了这两种快捷方式之间的区别。

Intent intent = new Intent(this, ShortcutActivity2.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("name", "孙三");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    if (mSm.isRequestPinShortcutSupported()) {
     // 验证设备是否支持//创建固定的快捷方式时,如果有这个快捷方式id,系统会自动绑定有关的所有信息,//如果要固定新的 就重新创建一个ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(this, "id").setShortLabel("固定的").setIntent(intent).build();Intent pinnedShortcutCallbackIntent = mSm.createShortcutResultIntent(pinShortcutInfo);PendingIntent successCallback = PendingIntent.getBroadcast(this, 0, pinnedShortcutCallbackIntent, 0);//尝试添加到桌面mSm.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());}
}

当固定到屏幕后,可以调用updateShortcuts()更新。

  相关解决方案