您好,欢迎来到花图问答。
搜索
您的当前位置:首页【 Android 】Android 7.1+ AppShort

【 Android 】Android 7.1+ AppShort

来源:花图问答

在 API 25,即 Android 7.1 的时候加入了 Shortcuts 这一功能。


image.png

官方文档解释:

The ShortcutManager manages an app's shortcuts. Shortcuts provide users with quick access to activities other than an app's main activity in the currently-active launcher. For example, an email app may publish the "compose new email" action, which will directly open the compose activity. The ShortcutInfo class contains information about each of the shortcuts themselves.

这一功能类似 iOS 的 3D Touch 功能。长按图标,(如果支持)就会弹出选项。
示例图片:

1.gif

实现这个功能有两种方式:静态 和 动态。

<b>静态 Shortcuts 的使用</b>
在 res 文件夹下 创建 xml 文件夹,并在其里面创建文件 shortcuts.xml

<?xml version="1.0" encoding="utf-8"?>
<shortcuts 
    

    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_one"
        android:shortcutDisabledMessage="@string/shortcut_disabled_message"
        android:shortcutId="one"
        android:shortcutLongLabel="@string/shortcut_one"
        android:shortcutShortLabel="@string/shortcut_one"
        tools:targetApi="n_mr1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.hoocent.shortcutsdemo.MainActivity"
            android:targetPackage="com.hoocent.shortcutsdemo" />
    </shortcut>
    ...
</shortcuts>

注:
① shortcutId, 不用多说, 这肯定是一个唯一的id
② enabled, 表示这个 shortcut 是否可用
③ shortcutShortLabel, 是配置的短名称, 下面还会有长名称, 如果长名称显示不下, 就显示短名称
④ shortcutLongLabel, 是配置的长名称, launcher会优先选择长名称显示
⑤ shortcutDisabledMessage,是在我们选择一个不可用的shortcut时给用户的一个提示
⑥ 必须将文字声明在 @string 里,否则直接将文字写在 " " 会报错。

最后在 AndroidManifest 里面添加(<activity>...</activity>里面):

<meta-data
    android:name="android.app.shortcuts"
    android:resource="@xml/shortcuts" />

</br>
<b>动态 ShortCuts 的使用</b>
创建一个方法 enableShortcuts() 实现动态 ShortCuts 的功能。

private void enableShortcuts() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        ShortcutManager manager = getSystemService(ShortcutManager.class);
        // Shortcuts 的静态+动态总数不能超过5个。
        ShortcutInfo one = new ShortcutInfo.Builder(this, "shortcut_one")
                .setLongLabel("ONE")    // 长名(优先显示长名)
                .setShortLabel("one")   // 短名(如果长名显示不下, 就显示短名)
                .setIcon(Icon.createWithResource(this, R.drawable.ic_one))
                .setIntent(
                        new Intent(MainActivity.this, MainActivity.class)   // 替换成你想要跳转的类
                                .setAction(Intent.ACTION_VIEW)
                                .addCategory(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION)
                ).build();
        ShortcutInfo two = new ShortcutInfo.Builder(this, "shortcut_two")
                .setLongLabel("TWO")
                .setShortLabel("two")
                .setIcon(Icon.createWithResource(this, R.drawable.ic_two))
                .setIntent(
                        new Intent(MainActivity.this, MainActivity.class)
                                .setAction(Intent.ACTION_VIEW)
                                .addCategory(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION)
                ).build();
        ShortcutInfo three = new ShortcutInfo.Builder(this, "shortcut_three")
                .setLongLabel("THREE")
                .setShortLabel("three")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher_round))
                .setIntent(
                        new Intent(MainActivity.this, MainActivity.class)
                                .setAction(Intent.ACTION_VIEW)
                                .addCategory(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION)
                ).build();
        manager.setDynamicShortcuts(Arrays.asList(one, two, three));
    }
}

最后将方法放在主类里面,一般放在 onCreate() 里。

</br>
<b>动态 Shortcuts 与静态 Shortcuts 区别</b>

  1. 静态 Shortcuts 只能通过升级应用修改,动态 Shortcuts 随时可以修改;
  2. 静态 Shortcuts 的 Intent 无法设置 Flag,默认为FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK Flag,即若应用运行中会清除所有已存在的 Activity。动态 Shortcuts 的 Intent 可以设置 Flag;
  3. 静态 Shortcuts 的rank系统默认根据声明顺序设置,动态 Shortcuts 的 rank 可以通过setRank(int rank) 接口主动设置,rank 不能小于 0,值越大表示在 shortcut 列表展示时离 App Icon 越远。静态 Shortcuts 默认比动态 Shortcuts 离 App Icon 更近。
  4. 静态 Shortcuts 删除可以直接删除,动态 Shortcuts 建议通过禁用删除;

Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务