[轉(zhuǎn)載文章]
長按桌面圖標(biāo)展示快捷方式,今時看來,早已司空見慣,一是Android很早的版本就已經(jīng)支持,二是大部分的應(yīng)用也已經(jīng)實現(xiàn),像微信,支付寶,頭條等,所以無論功能還是實現(xiàn)方式,都已經(jīng)踴躍出了大量的技術(shù)博文,但細(xì)細(xì)看去,卻很少有一個統(tǒng)一的流程及具體的實現(xiàn)方案,本文針對此功能做了細(xì)致的總結(jié),一是,便于日后開發(fā)的需要,二是,希望可以幫助到有類似需求的小伙伴。
這個特性,可以追溯到Android 7.1,也就是在7.1之后的系統(tǒng),如果app支持,可以通過長按app圖標(biāo)展示一些快捷操作,如下圖:

相信上圖中的功能,大家都見過,那么如何實現(xiàn)呢?Android API當(dāng)中給出了兩種實現(xiàn)方式,一種是靜態(tài),一種是動態(tài)。
靜態(tài)方式:
靜態(tài)的方式,需要xml資源,以shortcuts標(biāo)簽的形式引入,字面意思我們顯而易見,就是捷徑標(biāo)簽。
簡單兩步就可以實現(xiàn),第一步,在res目錄下,新建xml目錄,然后創(chuàng)建對應(yīng)的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標(biāo)簽, 里面就是包裹著一個一個快捷方式shortcut,你需要幾個,就創(chuàng)建幾個,上面代碼中我是創(chuàng)建了兩個,可以發(fā)現(xiàn)這些屬性和我們清單文件里的Activity里的屬性類似,這里簡單概述一下:
enabled, 表示這個shortcut是否可用
icon 為快捷圖標(biāo)
shortcutId, 快捷方式唯一的id
shortcutShortLabel, 短名稱
shortcutLongLabel, 這里是配置的長名稱, launcher會優(yōu)先選擇長名稱顯示,顯示不下會選擇短名稱
categories 為應(yīng)用程序的快捷方式執(zhí)行的操作類型提供分組,例如創(chuàng)建新的聊天消息
capability-binding 可選 聲明與此快捷方式關(guān)聯(lián)的功能。CREATE_MESSAGE 聲明的功能,是與應(yīng)用有關(guān)的 Action 內(nèi)置 intent。用戶可以結(jié)合使用語音指令與 Google 助理來調(diào)用此快捷方式。
在shortcut標(biāo)簽下,還有一個intent標(biāo)簽,不用說,想必大家也知道了它的作用,就是點擊快捷方式,跳轉(zhuǎn)的目標(biāo)。
intent, 這里表示我們點擊shortcut時要干嘛,
targetPackage是指定一個目標(biāo)應(yīng)用的包名,
targetClass是我們要跳轉(zhuǎn)的目標(biāo)類, 這里要注意的是android:action一定要配置, 否則會崩潰
categories, 這個東西目前位置官方只給提供了android.shortcut.conversation
第二步,清單文件AndroidManifest里進(jìn)行配置,這個需要注意一下:只能在有action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER的Activity中配置才有效,說簡單點,也就是應(yīng)用的主入口。
<!--引入shortcuts資源-->
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
以上兩步完成之后,我們就可以運行程序,效果如下:

動態(tài)方式:
上述的過程,我們實現(xiàn)了靜態(tài)的快捷方式,但常見的需求情況下,有很多是需要動態(tài)配置的,那么如何實現(xiàn)呢?其實也非常簡單,目前動態(tài)的方式創(chuàng)建其中,也有兩種代碼方式,一種是通過ShortcutManagerCompat來實現(xiàn),一種是ShortcutManager,兩種方式大同小異,我們一起來看下:
ShortcutManagerCompat方式實現(xiàn):
添加:
//動態(tài)方式添加一
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val shortScan = ShortcutInfoCompat.Builder(this, "test_2")//唯一標(biāo)識id
.setShortLabel(getString(R.string.app_test_2))//短標(biāo)簽
.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))//圖標(biāo)
//跳轉(zhuǎn)的目標(biāo),定義Activity
.setIntent(Intent(Intent.ACTION_MAIN, null, this, MainActivity::class.java))
.build()
//執(zhí)行添加操作
ShortcutManagerCompat.addDynamicShortcuts(this, mutableListOf(shortScan))
toast("已添加")
}
添加后效果對比

更新:
//動態(tài)更新方式一
val shortScan = ShortcutInfoCompat.Builder(this, "test_2")//唯一標(biāo)識id
.setShortLabel(getString(R.string.app_test_2_updata))//更新一個短標(biāo)簽
.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))//圖標(biāo)
//要跳轉(zhuǎn)的目標(biāo)
.setIntent(Intent(Intent.ACTION_MAIN, null, this, MainActivity::class.java))
.build()
//執(zhí)行更新操作
ShortcutManagerCompat.updateShortcuts(this, mutableListOf(shortScan))
toast("已更新")
更新前后效果對比

刪除:
//動態(tài)移除方式一
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManagerCompat.removeDynamicShortcuts(
this@MainActivity,
Collections.singletonList("test_2")//唯一標(biāo)識id
)
toast("已移除")
}
刪除后效果

ShortcutManager方式實現(xiàn):
添加:
//動態(tài)方式添加二
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val info = ShortcutInfo.Builder(this, "test_3")//唯一標(biāo)識id
.setShortLabel(getString(R.string.app_test_3))//短的標(biāo)簽
.setLongLabel(getString(R.string.app_test_3_long))//長的標(biāo)簽
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))//圖標(biāo)
.setIntent(intent)//跳轉(zhuǎn)的目標(biāo),這里我設(shè)置的是當(dāng)前
.build()
//執(zhí)行添加操作
getSystemService(ShortcutManager::class.java)
.dynamicShortcuts = mutableListOf(info)
toast("已添加")
}
刪除:
//動態(tài)移除方式二
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
getSystemService(ShortcutManager::class.java)
.removeDynamicShortcuts(listOf("test_3"))//唯一的id標(biāo)識
toast("已移除")
}
更新:
//動態(tài)更新方式二
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val info = ShortcutInfo.Builder(this, "test_3")//唯一標(biāo)識id
.setShortLabel(getString(R.string.app_test_3_updata))//更新一個短標(biāo)簽
.setLongLabel(getString(R.string.app_test_3_long))//長標(biāo)簽
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))//圖標(biāo)
.setIntent(intent)//跳轉(zhuǎn)的目標(biāo),這里我設(shè)置的是當(dāng)前
.build()
//執(zhí)行更新操作
getSystemService(ShortcutManager::class.java).updateShortcuts(listOf(info))
toast("已更新")
}
上述的代碼中,注釋已經(jīng)很清楚了,這里就不細(xì)講,效果呢和第一種方式類似,這里就不貼效果了,大家感興趣的話,可以直接看源碼,地址是:
https://github.com/AbnerMing888/AndroidWidget