Android App桌面長按菜單展示快捷操作

[轉載文章]

長按桌面圖標展示快捷方式,今時看來,早已司空見慣,一是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, 快捷方式唯一的id
shortcutShortLabel, 短名稱
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/AbnerMing888/AndroidWidget

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章