Android長按圖示展示快捷方式

語言: CN / TW / HK

highlight: a11y-dark

長按桌面圖示展示快捷方式,今時看來,早已司空見慣,一是Android很早的版本就已經支援,二是大部分的應用也已經實現,像微信,支付寶,頭條等,所以無論功能還是實現方式,都已經踴躍出了大量的技術博文,但細細看去,卻很少有一個統一的流程及具體的實現方案,本文針對此功能做了細緻的總結,一是,便於日後開發的需要,二是,希望可以幫助到有類似需求的小夥伴。

這個特性,可以追溯到Android 7.1,也就是在7.1之後的系統,如果app支援,可以通過長按app圖示展示一些快捷操作,如下圖:

image.png

相信上圖中的功能,大家都見過,那麼如何實現呢?Android API當中給出了兩種實現方式,一種是靜態,一種是動態,

靜態方式:

靜態的方式,需要xml資源,以shortcuts標籤的形式引入,字面意思我們顯而易見,就是捷徑標籤。

簡單兩步就可以實現,第一步,在res目錄下,新建xml目錄,然後建立對應的xml資源。

image.png

```xml

<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標籤, 裡面就是包裹著一個一個快捷方式shortcut,你需要幾個,就建立幾個,上面程式碼中我是建立了兩個,可以發現這些屬性和我們清單檔案裡的Activity裡的屬性類似,這裡簡單概述一下:

js enabled, 表示這個shortcut是否可用 icon 為快捷圖示 shortcutId, 快捷方式唯一的id shortcutShortLabel, 短名稱 shortcutLongLabel, 這裡是配置的長名稱, launcher會優先選擇長名稱顯示,顯示不下會選擇短名稱 categories 為應用程式的快捷方式執行的操作型別提供分組,例如建立新的聊天訊息 capability-binding 可選 宣告與此快捷方式關聯的功能。CREATE_MESSAGE 宣告的功能,是與應用有關的 Action 內建 intent。使用者可以結合使用語音指令與 Google 助理來呼叫此快捷方式。 在shortcut標籤下,還有一個intent標籤,不用說,想必大家也知道了它的作用,就是點選快捷方式,跳轉的目標。

js intent, 這裡表示我們點選shortcut時要幹嘛, targetPackage是指定一個目標應用的包名, targetClass是我們要跳轉的目標類, 這裡要注意的是android:action一定要配置, 否則會崩潰 categories, 這個東西目前位置官方只給提供了android.shortcut.conversation 第二步,清單檔案AndroidManifest裡進行配置,這個需要注意一下:只能在有action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER的Activity中配置才有效,說簡單點,也就是應用的主入口。

```js

``` 以上兩步完成之後,我們就可以執行程式,效果如下:

image.png

動態方式:

上述的過程,我們實現了靜態的快捷方式,但常見的需求情況下,有很多是需要動態配置的,那麼如何實現呢?其實也非常簡單,目前動態的方式建立其中,也有兩種程式碼方式,一種是通過ShortcutManagerCompat來實現,一種是ShortcutManager,兩種方式大同小異,我們一起來看下:

ShortcutManagerCompat方式實現:

新增:

```kotlin //動態方式新增一 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("已新增")

} ``` 新增後效果對比

image.png

更新:

```kotlin //動態更新方式一 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("已更新") ``` 更新前後效果對比

image.png

刪除:

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

image.png

ShortcutManager方式實現:

新增:

```kotlin //動態方式新增二 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("已新增")

} ```

刪除:

kotlin //動態移除方式二 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { getSystemService(ShortcutManager::class.java) .removeDynamicShortcuts(listOf("test_3"))//唯一的id標識 toast("已移除") } 更新:

```kotlin //動態更新方式二 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("已更新")

} ``` 上述的程式碼中,註釋已經很清楚了,這裡就不細講,效果呢和第一種方式類似,這裡就不貼效果了,大家感興趣的話,可以直接看原始碼,地址是:

http://github.com/AbnerMing888/AndroidWidget