AndroidShortcuts使用详解

7.1的时候出现了一个长按图标出现的功能列表的效果,类似于2015年ios的3dTouch功能.奈何国产,大家懂得,到目前用了一加五才可以感受这个功能的好处.

效果图来了

这里写图片描述

shortcuts的介绍

其中App Shortcuts是指在桌面长按app图标而出现的快捷方式, 可以为你的app的关键功能添加更快速的入口而不用先打开app,点击快捷方式可以访问应用功能, 并且这种快捷方式也可以被拖拽到桌面单独放置, 变成单独的桌面快捷方式.

有两种shortcuts:

  • 静态的: 在xml中定义, 适用于一些通用的动作.
  • 动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.

数量

每一个应用目前最多可以有5个shortcuts(静态 + 动态)

运行条件

应用添加App Shortcuts是Android 7.1(API 25)的API, 所以只能在Android 7.1的设备上显示, 同时需要launcher支持, 比如Pixel launcher(Pixel设备的默认launcher), Now launcher(Nexus设备上的launcher)现在就支持, 其他launcher也可以提供支持.

静态的shortcuts的使用

清单文件配置

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!--添加支持shortcuts-->
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
        </activity>

在res目录下添加xml文件夹,并新建文件shortcuts

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:icon="@drawable/add"
        android:shortcutId="add_website"
        android:shortcutLongLabel="@string/add_new_website"
        android:shortcutShortLabel="@string/add_new_website_short">
        <!--最左侧或者添加桌面后显示的图标-->
        <!--id标识-->
        <!--长按显示的文字-->
        <!--添加到桌面显示的文字-->
        <intent
            android:action="cn.yky.test.ADD_WEBSITE"
            android:targetClass="cn.yky.test.MainActivity"
            android:targetPackage="cn.yky.test" />
        <!--第一个是意图标识-->
        <!--第二个参数是希望打开的界面-->
        <!--希望打开界面的包名-->
    </shortcut>
</shortcuts>

注意,shortcutLongLabel和shortcutShortLabel,不可以直接引用文字,不然会报错.!!!谁加谁知道.经过以上的步骤之后,就可以看到最开始的效果图了!.

动态添加

动态的shortcuts可以在用户使用app的过程中构建, 更新, 或者删除.

ShortcutManager

  • 设置或者新增 setDynamicShortcuts; addDynamicShortcuts;
  • 修改 updateShortcuts;
  • 删除 removeDynamicShortcuts; removeAllDynamicShortcuts;

动态添加三个

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            mShortcutManager = getSystemService(ShortcutManager.class);
            getNewShortcutInfo();
        }

 /**
     * 动态添加三个
     */
    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    private void getNewShortcutInfo() {
        ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
                .setShortLabel("Web site")
                .setLongLabel("第一个")
                .setIcon(Icon.createWithResource(this, R.drawable.link))
                .setIntent(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://www.baidu.com/")))
                .build();
        ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "id2")
                .setShortLabel("Web site")
                .setLongLabel("第二个")
                .setIcon(Icon.createWithResource(this, R.drawable.link))
                .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.drawable.link))
                .setIntent(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://www.github.com/")))
                .build();
        mShortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2, shortcut3));
    }

效果图

这里写图片描述

依次类推,就可以动态增删改我们的快捷方式了.虽然你想用,但是国产手机估计还得一年以内的时间能普及到吧.


联系方式

本人技术有限,还有很多不完美的地方,欢迎指出.(写作不易,谢谢您的star支持)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值