Android7.1图标快捷方式(AppShortcuts)实现Demo
今天给手上的Nexus6P手机升级系统至Android7.1,体验了一下新功能:App Shortcuts(图标快捷方式),如下图所示:
如何实现这样的快捷方式呢?
官方给出的实现步骤:App Shortcuts | Android Developers
分类
图标快捷方式(App Shortcuts)分为两种:
Static shortcuts(静态快捷方式)
Dynamic shortcuts(动态快捷方式)
静态快捷方式是写在xml文件中,而动态快捷方式是在Java代码中编写.
实现
环境要求
只有Android7.1(25)及以上手机才能使用该功能
要求SDK版本为25及以上
最低兼容版本为23(因为动态快捷方式使用Icon类)
module 的build.gradle配置如下:
apply plugin: 'com.android.application'android {compileSdkVersion 25buildToolsVersion "25.0.0"defaultConfig {applicationId "net.devwiki.shortcuts"minSdkVersion 23targetSdkVersion 25versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}
}dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.android.support:appcompat-v7:25.0.0'testCompile 'junit:junit:4.12'
}
静态快捷方式
1.在项目的res/文件夹下建立 xml目录
2.在xml目录创建shortcuts.xml文件,内容如下
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcutandroid:shortcutId="compose"android:enabled="true"android:icon="@drawable/ic_share_red_700_48dp"android:shortcutShortLabel="@string/activity_short_label"android:shortcutLongLabel="@string/activity_long_label"android:shortcutDisabledMessage="@string/activity_disable_message"><intentandroid:action="android.intent.action.VIEW"android:targetPackage="net.devwiki.shortcuts"android:targetClass="net.devwiki.shortcuts.StaticActivity" /><!-- If your shortcut is associated with multiple intents, include themhere. The last intent in the list is what the user sees when theylaunch this shortcut. --><categories android:name="android.shortcut.conversation" /></shortcut><!-- Specify more shortcuts here. -->
</shortcuts>
属性含义和图片说明如下:
shortcutId : 快捷方式的ID
enabled : 是否可用,不可用时点击Toast提示shortcutDisabledMessage
icon : 快捷方式图标
shortcutShortLabel : 快捷图标放置到桌面时的标题
shortcutLongLabel : 长按图标出现快捷方式时的标题
shortcutDisabledMessage : 快捷图标禁用时的提示语
3.在AndroidManifest.xml文件中设置静态快捷方式
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="net.devwiki.shortcuts"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"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=".StaticActivity"></activity></application>
</manifest>
动态快捷方式
动态快捷方式即在代码中添加,更新快捷图标.
ShortcutManager
提供了添加,移除,更新,禁用,启动,获取静态快捷方式,获取动态快捷方式,获取固定在桌面的快捷方式等方法.
可在Java代码中动态对图标快捷方式进行操作.
添加快捷方式
private void addShortcut() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {List<ShortcutInfo> infoList = shortcutManager.getDynamicShortcuts();String shortcutId = null;for (ShortcutInfo shortcutInfo : infoList) {if (ShortcutsId.WEB_DEVWIKI.equals(shortcutInfo.getId())) {shortcutId = shortcutInfo.getId();}}if (shortcutId == null) {ShortcutInfo shortcut = new ShortcutInfo.Builder(this, ShortcutsId.WEB_DEVWIKI).setShortLabel(getString(R.string.blog_short_label)).setLongLabel(getString(R.string.blog_long_label)).setIcon(Icon.createWithResource(this, R.drawable.ic_blog_logo)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.devwiki.net"))).build();shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));Toast.makeText(this, R.string.add_shortcut_success, Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, R.string.shortcut_already_exist, Toast.LENGTH_SHORT).show();}} else {Toast.makeText(this, R.string.not_support_function, Toast.LENGTH_SHORT).show();}
}
移除快捷方式
private void removeShortcut() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {List<ShortcutInfo> infoList = shortcutManager.getDynamicShortcuts();String shortcutId = null;for (ShortcutInfo shortcutInfo : infoList) {if (ShortcutsId.WEB_DEVWIKI.equals(shortcutInfo.getId())) {shortcutId = shortcutInfo.getId();}}if (shortcutId == null) {Toast.makeText(this, R.string.shortcut_not_exist, Toast.LENGTH_SHORT).show();} else {shortcutManager.removeDynamicShortcuts(Arrays.asList(shortcutId));Toast.makeText(this, R.string.remove_shortcut_success, Toast.LENGTH_SHORT).show();}} else {Toast.makeText(this, R.string.not_support_function, Toast.LENGTH_SHORT).show();}
}
项目代码
项目代码在此处:Shortcuts
Android7.1图标快捷方式(AppShortcuts)实现Demo相关推荐
- win10系统桌面图标变为白色方框和去除桌面图标快捷方式小箭头的方法
一.有些用户在开机后,发现windows10系统桌面的图标都变成了白色的方框.这是怎么回事?下面就为大家介绍一下Win10系统桌面图标变为白色方框的解决办法. 导致win10图标变白的情况有三种,大家 ...
- linux系统安装pycharm的桌面图标快捷方式图标是空白的解决方案
#linux系统安装pycharm的桌面图标快捷方式图标是空白的解决方案 ##注意点,不管是centos,还是ubuntu,pycharm一定要放到computer上,不然容易出现图标为空白 cd / ...
- 是时候来了解android7了:shortcuts(快捷方式)
本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发. 就在前几天的一个晚上, Google召开了它的秋季发布会, 毫无悬念的宣布了它的最新手机品牌Pixel, 与此同 ...
- 安卓Android 7.1.1 shortcut实现桌面图标快捷方式跳转,类似IOS 3d touch
一.背景介绍: 3D Touch是一种立体触控技术,被苹果称为新一代多点触控技术,是在Apple Watch上采用的Force Touch,屏幕可感应不同的感压力度触控.3D Touch,苹果iPho ...
- Ubuntu系统创建AndroidStudio启动图标(快捷方式)
Ubuntu 的快捷方式都在/usr/share/applications/路径的,可以cd到这个目录ls 查看下 创建androidstudio 的快捷方式同样也是在这个目录下面 1 首先这这个目录 ...
- win10/win11快速隐藏/显示桌面图标快捷方式
使用windows系统的时候,桌面有时会变得比较杂乱,想要随时随地很方便的隐藏和显示桌面图标还不是不是一件容易的事情,这里介绍了一个代码,可以很方便地设置快捷方式. 快捷键alt+q隐藏/显示 ...
- deepin添加启动图标(快捷方式)
在linux(deepin)中,由深度商店下载的应用会自动在启动器创建快捷方式,但是从浏览器上下载的应用就只能找到安装后的目录,点击再打开,很不方便,在这里就分享下我找到的在启动器上创建启动图标的方式 ...
- 7. Linux系统下在桌面设置添加安装软件启动图标快捷方式
1. 说明 在Linux系统下安装了对应的软件之后,一般启动时需要在命令行终端中进行启动,可以在终端里设置快速启动的命令,相关设置可参考博客:Linux系统下在终端设置快速启动已安装软件. 另一种快捷 ...
- Android桌面长按图标快捷方式——Shortcuts
简介 当我们在长按Android应用的桌面图标时,一般回弹出一个列表,上面一般有应用信息.卸载应用等功能,并且部分应用在这里还添加了自己的快捷方式,今天主要介绍如何添加自定义的快捷方式. 长按桌面显示 ...
最新文章
- 简单轻松学 Linux 之 awk
- python中parse是什么_python中的configparse学习笔记
- curl -windows下接口通讯
- 前端学习(2176):vue-router的路由的嵌套使用
- odoo 中多币种处理(外币处理)
- 线程基础知识_线程生命周期_从JVM内存结构看多线程下的共享资源
- Keras中LSTM的return_sequences和return_state
- 史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码...
- 【每日算法Day 63】LeetCode 第 179 场周赛题解
- 解决springboot的application.yml配置不生效问题
- 用proxifier、ccproxy、teamviewer做网络代理
- Java Instrument实践应用:运行中修改程序的Class
- 【滴滴拉屎】一款能按照坑型找厕所的神器!
- php 返回英文乱码,使用php 5时MySQL返回乱码的解决办法_php
- xml 入门 shema_02
- android html字体大小,android Html.fromHtml font 标签支持设置字体大小和颜色
- pg 百万数据表 添加序号 20秒轻松搞定
- 项目起名的一些小单词
- 局域网文件服务器怎么备份数据,局域网文件备份软件的使用教程
- torch各个版本镜像_Anaconda安装Pytorch镜像详细教程
热门文章
- 打算把我的视频工具整合一下
- MD5数据加密于文件加密
- 关于vs中代码生成的运行库
- 快速上手笔记,PyTorch模型训练实用教程(附代码)
- spring Cache /Redis 缓存 + Spring 的集成示例
- 关闭PdfReader右侧工具栏的方法
- 高通Vuforia优化目标检测与跟踪稳定性
- 《iOS 9应用开发入门经典(第7版)》——第1章,第1.6节小结
- 上struts2的xml在lt;result type=quot;redirectquot;gt;参数问题
- 【翻译】如何创建Ext JS暗黑主题之一