长按桌面图标展现快捷方式,今时看来,早已司空见惯,一是Android很早的版本就曾经反对,二是大部分的利用也曾经实现,像微信,支付宝,头条等,所以无论性能还是实现形式,都曾经踊跃出了大量的技术博文,但细细看去,却很少有一个对立的流程及具体的实现计划,本文针对此性能做了粗疏的总结,一是,便于日后开发的须要,二是,心愿能够帮忙到有相似需要的小伙伴。
这个个性,能够追溯到Android 7.1,也就是在7.1之后的零碎,如果app反对,能够通过长按app图标展现一些快捷操作,如下图:
置信上图中的性能,大家都见过,那么如何实现呢?Android API当中给出了两种实现形式,一种是动态,一种是动静,
动态形式:
动态的形式,须要xml资源,以shortcuts标签的模式引入,字面意思咱们不言而喻,就是捷径标签。
简略两步就能够实现,第一步,在res目录下,新建xml目录,而后创立对应的xml资源。
<?xml version="1.0" encoding="utf-8"?><shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:enabled="true" android:icon="@mipmap/ic_launcher" android:shortcutId="test_0" android:shortcutShortLabel="@string/app_test_0"> <intent android:action="android.intent.action.VIEW" android:targetClass="com.abner.widget.Test0Activity" android:targetPackage="com.abner.widget" /> <categories android:name="android.shortcut.conversation" /> <capability-binding android:key="actions.intent.CREATE_MESSAGE" /> </shortcut> <shortcut android:enabled="true" android:icon="@mipmap/ic_launcher" android:shortcutId="test_1" android:shortcutShortLabel="@string/app_test_1"> <intent android:action="android.intent.action.VIEW" android:targetClass="com.abner.widget.Test1Activity" android:targetPackage="com.abner.widget" /> <categories android:name="android.shortcut.conversation" /> <capability-binding android:key="actions.intent.CREATE_MESSAGE" /> </shortcut></shortcuts>
外层首先一个shortcuts标签, 外面就是包裹着一个一个快捷方式shortcut,你须要几个,就创立几个,下面代码中我是创立了两个,能够发现这些属性和咱们清单文件里的Activity里的属性相似,这里简略概述一下:
enabled, 示意这个shortcut是否可用icon 为快捷图标shortcutId, 快捷方式惟一的idshortcutShortLabel, 短名称shortcutLongLabel, 这里是配置的长名称, launcher会优先选择长名称显示,显示不下会抉择短名称categories 为应用程序的快捷方式执行的操作类型提供分组,例如创立新的聊天音讯capability-binding 可选 申明与此快捷方式关联的性能。CREATE_MESSAGE 申明的性能,是与利用无关的 Action 内置 intent。用户能够联合应用语音指令与 Google 助理来调用此快捷方式。
在shortcut标签下,还有一个intent标签,不用说,想必大家也晓得了它的作用,就是点击快捷方式,跳转的指标。
intent, 这里示意咱们点击shortcut时要干嘛, targetPackage是指定一个指标利用的包名, targetClass是咱们要跳转的指标类, 这里要留神的是android:action肯定要配置, 否则会解体categories, 这个货色目前地位官网只给提供了android.shortcut.conversation
第二步,清单文件AndroidManifest里进行配置,这个须要留神一下:只能在有action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER的Activity中配置才无效,说简略点,也就是利用的主入口。
<!--引入shortcuts资源--><meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
以上两步实现之后,咱们就能够运行程序,成果如下:
动静形式:
上述的过程,咱们实现了动态的快捷方式,但常见的需要状况下,有很多是须要动静配置的,那么如何实现呢?其实也非常简单,目前动静的形式创立其中,也有两种代码形式,一种是通过ShortcutManagerCompat来实现,一种是ShortcutManager,两种形式大同小异,咱们一起来看下:
ShortcutManagerCompat形式实现:
增加:
//动静形式增加一if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { val shortScan = ShortcutInfoCompat.Builder(this, "test_2")//惟一标识id .setShortLabel(getString(R.string.app_test_2))//短标签 .setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))//图标 //跳转的指标,定义Activity .setIntent(Intent(Intent.ACTION_MAIN, null, this, MainActivity::class.java)) .build() //执行增加操作 ShortcutManagerCompat.addDynamicShortcuts(this, mutableListOf(shortScan)) toast("已增加")}
增加后成果比照
更新:
//动静更新形式一val shortScan = ShortcutInfoCompat.Builder(this, "test_2")//惟一标识id .setShortLabel(getString(R.string.app_test_2_updata))//更新一个短标签 .setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))//图标 //要跳转的指标 .setIntent(Intent(Intent.ACTION_MAIN, null, this, MainActivity::class.java)) .build()//执行更新操作ShortcutManagerCompat.updateShortcuts(this, mutableListOf(shortScan))toast("已更新")
更新前后成果比照
删除:
//动静移除形式一if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutManagerCompat.removeDynamicShortcuts( this@MainActivity, Collections.singletonList("test_2")//惟一标识id ) toast("已移除")}
删除后成果
ShortcutManager形式实现:
增加:
//动静形式增加二if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { val info = ShortcutInfo.Builder(this, "test_3")//惟一标识id .setShortLabel(getString(R.string.app_test_3))//短的标签 .setLongLabel(getString(R.string.app_test_3_long))//长的标签 .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))//图标 .setIntent(intent)//跳转的指标,这里我设置的是以后 .build() //执行增加操作 getSystemService(ShortcutManager::class.java) .dynamicShortcuts = mutableListOf(info) toast("已增加")}
删除:
//动静移除形式二if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { getSystemService(ShortcutManager::class.java) .removeDynamicShortcuts(listOf("test_3"))//惟一的id标识 toast("已移除")}
更新:
//动静更新形式二if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { val info = ShortcutInfo.Builder(this, "test_3")//惟一标识id .setShortLabel(getString(R.string.app_test_3_updata))//更新一个短标签 .setLongLabel(getString(R.string.app_test_3_long))//长标签 .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))//图标 .setIntent(intent)//跳转的指标,这里我设置的是以后 .build() //执行更新操作 getSystemService(ShortcutManager::class.java).updateShortcuts(listOf(info)) toast("已更新")}
上述的代码中,正文曾经很分明了,这里就不细讲,成果呢和第一种形式相似,这里就不贴成果了,大家感兴趣的话,能够间接看源码,地址是:
https://github.com/AbnerMing8...