Android 8.0 应用快捷方式(ShortcutManager)的使用

Posted JackWaiting

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 8.0 应用快捷方式(ShortcutManager)的使用相关的知识,希望对你有一定的参考价值。

android 7.1(API 25)之后添加的新功能,应用快捷方式。ShortcutManager管理一个应用程序的快捷方式。只要长按APP图标支持快捷方式,通过快捷键,用户可以快速访问任意一个Activity。

简单地理解:在长按应用图标的情况下,在应用图标上显示的快捷方式,该快捷方式可以点击进入Activity,长按拖动创建一个在Launcher上的图标。

现在市场上已经是有很多应用增加了这项功能,如:支付宝、知乎、印象笔记、美团等,大概的效果如下图:

快捷方式的特点

静态和动态快捷方式

用户执行特定手势时,支持的手机中会显示静态快捷方式和动态快捷方式。 在7.1、8.0系统中,大部分手势是对应用程序图标的长按,但实际手势可能与其他手机应用程序不同。

重要的安全注意事项:所有的快捷方式信息都存储在凭证加密存储中,因此您的应用只有在解锁设备后才能访问用户的快捷方式。

静态方式

静态方式就是通过xml文件进行配置,就像ActionBar的选项一样。
先配置快捷方式的选项:
res/xml/static_shortcuts.xml 主要配置如下参数:id、图标、标题、意图,还有分类,是否可用。

<?xml version="1.0" encoding="utf-8"?>
    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
        <!--搜索-->
        <shortcut
            android:enabled="true"
            android:icon="@drawable/ic_bnsports"
            android:shortcutDisabledMessage="@string/lable_shortcut_static_search_disable"
            android:shortcutId="shortcut_id_search"
            android:shortcutLongLabel="@string/lable_shortcut_static_search_long"
            android:shortcutShortLabel="@string/lable_shortcut_static_search_short">
            <intent
                android:action="android.intent.action.VIEW"
                android:targetClass="com.example.song.jackwaiting.MainActivity"
                android:targetPackage="com.example.song.jackwaiting" />
            <!--当前只有这个分类-->
            <categories android:name="android.shortcut.conversation" />
        </shortcut>
    </shortcuts>

配置完图标的之后,再打开AndroidManiFest.xml配置启动的Activity
AndroidManiFest.xml

<activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
           <!--add static shortcut-->
           <meta-data android:name="android.app.shortcuts"
               android:resource="@xml/static_shortcut" />
       </activity>

动态方式

那么动态方式,是通过ShortcutManager实现快捷方式的增加、删除、更新的操作,使用起来很简单。下面就放一下简单的配置代码。

/**
         * 先生成一个一个的快捷方式的对象
         * 对象需要配置:图标、标题、意图,其他的配置具体可以到官网查看
         */

        if (Build.VERSION.SDK_INT >= 25) 
            ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, SHORTCUT_ID_SEARCH)
                    .setShortLabelResId(R.string.lable_shortcut_static_search_long)
                    .setLongLabelResId(R.string.lable_shortcut_static_search_long)
                    .setIcon(Icon.createWithResource(this, R.drawable.ic_bnsports))
                    .setIntent(new Intent(this, MainActivity.class))
                    .build();

            ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
            //这样就可以通过长按图标显示出快捷方式了
            shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));

        

配置好之后看一下效果:

常用的方法:

移除快捷方式
void removeDynamicShortcuts(@NonNull List shortcutIds);

移除快捷方式,并且拖到launcher上的图标会变灰,这个方法底层最终调用的方法跟removeDynamicShortcuts一样
void disableShortcuts(@NonNull List shortcutIds);

使拖到launcher的图标可用。 静态添加的快捷方式不能使用该方法,使用会报错
void enableShortcuts(@NonNull List shortcutIds);

更新快捷方式,例如图标、标题
boolean updateShortcuts(List shortcutInfoList);

显示的顺序问题:添加在前的显示在下方,后面添加的显示在上面。

这个类的使用还是比较简单的。添加了这项功能的APP,长按的效果跟ios的3D touch差不多的视觉体验。

LauncherApps类为启动程序应用程序提供访问快捷方式的API。

最后附上官网地址:
https://developer.android.google.cn/reference/android/content/pm/ShortcutManager.html

以上是关于Android 8.0 应用快捷方式(ShortcutManager)的使用的主要内容,如果未能解决你的问题,请参考以下文章

Android应用快捷方式

Installshield创建快捷方式不能正常运行的几种原因

Android 应用程序未从快捷方式启动

Android-8.0系统中应用图标的适配

主屏幕应用程序快捷方式不适用于 android。未安装应用程序

新手为Android创建URL应用程序的快捷方式[重复]