转载请注明出处:王亟亟的大牛之路

前些天就看到相关内容了,但是最近吸毒比较深(wow),所以没有紧跟潮流,今天补一篇。

先安利:https://github.com/ddwhan0123/Useful-Open-Source-Android [408 star]

看下效果:

肉眼看就是多了一排列表,是一些可点击的按钮,可定制一些常用的方便用户操作的快捷键。

理论知识翻译自官网,有基础好的同学可以直接看:https://developer.android.com/preview/shortcuts.html


啰里八嗦的文本介绍就不提了,说下怎么用合一些规范

他有2种加载方式

1.静态加载

2.动态加载

静态的方式可以兼容低版本,动态的暂时只支持7.1

字面就很好理解,静态的就是事先编辑好展示ui,跳转逻辑等等。

动态就是可以临时调用。


Static Shortcuts

在AndroidManifest.xml文件,首页activity的节点里的</intent-filter>下添加

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

shortcuts 其实就是我们静态编辑的内容,类似于预设Menu的概念


加完之后就是编辑shortcuts这个xml了,他要在 res/xml/shortcuts.xml 这个位置

例子中的文件清单如下

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:shortcutId="shortcut1"
        android:enabled="true"
        android:icon="@drawable/happy"
        android:shortcutShortLabel="@string/one_text"
        android:shortcutLongLabel="@string/one_long"
        android:shortcutDisabledMessage="@string/one_disabled">
        <intent
            android:action="one"
            android:targetPackage="demo.wjj.shortcutsdemo"
            android:targetClass="demo.wjj.shortcutsdemo.MainActivity" />
    </shortcut>

    <shortcut
        android:shortcutId="shortcut2"
        android:enabled="false"
        android:icon="@drawable/woman"
        android:shortcutShortLabel="@string/two"
        android:shortcutLongLabel="@string/two_long"
        android:shortcutDisabledMessage="@string/two_disabled">
        <intent
            android:action="two"
            android:targetPackage="demo.wjj.shortcutsdemo"
            android:targetClass="demo.wjj.shortcutsdemo.MainActivity" />
    </shortcut>

    <shortcut
        android:shortcutId="shortcut3"
        android:enabled="true"
        android:icon="@drawable/angry"
        android:shortcutShortLabel="@string/three"
        android:shortcutLongLabel="@string/three_long"
        android:shortcutDisabledMessage="@string/three_disabled">
        <intent
            android:action="three"
            android:targetPackage="demo.wjj.shortcutsdemo"
            android:targetClass="demo.wjj.shortcutsdemo.MainActivity" />
    </shortcut>

</shortcuts>

自行设置包名,类名,icon,描述文字等。

action对应的就是你点完快捷键回到activity时作判断的”key”

例子里第二个”item”没显示出来也就是因为android:enabled设置了false

其他的你只要在业务界面 getIntent().getActiob()就行了,so easy


Dynamic Shortcuts

动态的加载方式就相对麻烦一点,但是代码更活,官方提到的常用方法如下

setDynamicShortcuts(List)  重新设置动态快捷方式的列表。

addDynamicShortcuts(List) 添加到已存在的快捷方式列表。

updateShortcuts(List) 更新列表。

removeDynamicShortcuts(List) 移除快捷方式。

removeAllDynamicShortcuts() 移除全部快捷方式。

然后他举了个跳转网页的例子

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
    .setShortLabel("Web site")
    .setLongLabel("Open the web site")
    .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
    .setIntent(new Intent(Intent.ACTION_VIEW,
                   Uri.parse("https://www.mysite.example.com/")))
    .build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

然后就是一堆规范啊,要求啊,设计的,这边不做详细解释,直接简单明了的概括下

按照快捷键设计指南 传送门:https://material.google.com/style/icons.html#icons-launcher-shortcut-icons

只发布四个不同的快捷键:最多可以发5个,但是太长的话很丑,所以最多就放4个item

极限快捷描述长度:字数不要太多,长了也放不下外加占地方,尽量精简,如果可能的话,限制快捷方式的“简短描述”的长度为10个字符,并限制“长说明”的长度为25个字符。

维持快捷和行动使用历史:对于您创建的每个快捷方式,可以考虑在其中一个用户可以在应用程序内直接完成相同的任务的不同方法。

更新快捷方式,只有当它们的含义被保留

动态快捷键备份过程中不保留和恢复:动态快捷键不保留在设备经历了备份和恢复操作。


——————-我是华丽的分割线——————–

以下内容不看,demo跑了也看不到效果!!!!

其实这些都还好,慢慢倒持研究下就好,但是世界更新的太快,国人还在 安卓 4 5间徘徊,本宝宝没有7.1啊怎么跑?

在各方咨询后找到了一个兼容桌面,可以还原模拟谷歌桌面哦。

在不自定义快捷键的情况下,它自带会有一个快捷键



官网地址:http://www.apkmirror.com/apk/teslacoil-software/nova-launcher/

如果你懒,也可以走我的传送门:https://github.com/ddwhan0123/BlogSample/blob/master/ShortcutsDemo/com.teslacoilsw.launcher_5.0-beta8-49908_minAPI16(nodpi)_apkmirror.com.apk?raw=true

源码地址:https://github.com/ddwhan0123/BlogSample/tree/master/ShortcutsDemo

下载地址:https://github.com/ddwhan0123/BlogSample/blob/master/ShortcutsDemo/ShortcutsDemo.zip?raw=true

相关资料:http://www.androidcentral.com/how-use-app-shortcuts-android-71-google-pixel

发完后被吐槽后想起来,其实官方有sample….瞬间石化,但是写都写了,补个传送门吧https://developer.android.com/samples/AppShortcuts/project.html

对了,桌面那个谢谢 @烧饼

Android 7.1 快捷方式 Shortcuts的更多相关文章

  1. Android 7.1 - App Shortcuts

    Android 7.1 - App Shortcuts 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Shortcuts 文中如有纰漏,欢迎大家留言 ...

  2. Android 7.1 App Shortcuts使用

    Android 7.1 App Shortcuts使用 Android 7.1已经发了预览版, 这里是API Overview: API overview. 其中App Shortcuts是新提供的一 ...

  3. Android 桌面生成快捷方式

    Android生成桌面快捷方式的几种方法: //------------以下为动态替换桌面应用Icon的一种解决方案------------------- // 1.获取本地目录图片的Bitmap ; ...

  4. (转)Android创建桌面快捷方式两种方法

    [IT168技术]Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成. 谈谈在桌面上直接生成.个人觉得这个比较爽快,既然都是快捷方式了干嘛还 ...

  5. android 添加桌面快捷方式

    .在桌面创建快捷方式方法: 方法一:通过长按某一个应用程序的图标在桌面上创建启动该应用程序的快捷方式. 这个方法安装完程序都用户都能实现. 方法二:在应用程序中构建一个Intent,然后以Broadc ...

  6. Android 添加桌面快捷方式操作

    /** * 为程序创建桌面快捷方式 */ private void addShortcut(){ Intent shortcut = new Intent(“com.android.launcher. ...

  7. Android -- 创建桌面快捷方式

    代码                                                                                    /** * * 返回添加到桌 ...

  8. Android创建桌面快捷方式

    在桌面上创建特定界面的快捷入口,icon和title根据请求参数命名.在网上收集的一些相关资 料,在使用intent发送广播的时候,一些型号的收集会有问题,如魅族MX,红米,以及华为,使用setCla ...

  9. Android添加桌面快捷方式的简单实现

    核心代码如下: Button bn = (Button) findViewById(R.id.bn); // 为按钮的单击事件添加监听器 bn.setOnClickListener(new OnCli ...

随机推荐

  1. Oracle之rman常用命令及维护(51CTO风哥rman课程)

    list 查看数据库备份的信息 查询数据库对应物 list incarnation; list backup summary; 列出当前备份信息及汇总 B是备份 F是全备 A是归档 第三个A是是否有效 ...

  2. 2017-2018-2 20165330 实验三《敏捷开发与XP实现》实验报告

    实验内容 P基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件开发:即将软件需求分析.软件设计.软件构建.软件测试和软件维护这些相关技术和过程统一到一个体系中 敏捷开发:是一种以人为核 ...

  3. 修改NavigationBar的分根线颜色

    [self.navigationController.navigationBar setShadowImage:[Static ColorToImage:[Static colorWithHexStr ...

  4. squee_spoon and his Cube VI---郑大校赛(求最长子串)

    市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...

  5. maven 整合 ssm 异常分析

    异常一:使用tomcat 7 启动没问题访问(JSP)页面就报错:org.apache.jasper.JasperException: Unable to compile class for JSP ...

  6. 005-maven坐标和依赖

    1.何为Maven坐标 groupId.artifactId.version.packaging.classifier 中央仓库:http://repol.maven.org/maven22.坐标详解 ...

  7. C的指针疑惑:C和指针17(经典抽象数据类型)

    堆栈这种数据最鲜明的特点是:后进先出. 用动态数组实现堆栈: #include "C17.h" #include <stdio.h> #include <stdl ...

  8. 栈的最大值问题 max问题 min问题 队列的max问题

    常数时间求栈的最大值   问题描述: 一个栈stack,具有push和pop操作,其时间复杂度皆为O(1). 设计算法max操作,求栈中的最大值,该操作的时间复杂度也要求为O(1). 可以修改栈的存储 ...

  9. python 中list的操作(循环、切片、增、删、改、查、反转、排序)

    列表的索引(下标)从0开始,最后一个可以用-1表示. 1. 循环 如果直接for 循环一个list 的时候,那么每次循环的都是这个List里的元素 2. 切片 可指定步长进行取值,步长默认为1 3. ...

  10. By.Xpath快速定位页面元素常用方法

    先看一看xpath的语法 我们将在下面的例子中使用这个 XML 文档. <?xml version="1.0" encoding="ISO-8859-1" ...