Android App Shortcuts使用說明(長按快捷方式)

Shortcut

其中App Shortcuts是指在桌面長按app圖標而出現(xiàn)的快捷方式, 可以為你的app的關(guān)鍵功能添加更快速的入口而不用先打開app,點擊快捷方式可以訪問應(yīng)用功能, 并且這種快捷方式也可以被拖拽到桌面單獨放置, 變成單獨的桌面快捷方式.

典型應(yīng)用

支付寶&騰訊新聞(每次雖然國內(nèi)跟進的晚,但是阿里騰訊也算是最早跟進的一批了)


支付寶

騰訊新聞

使用方式

兩種方式使用

  • 靜態(tài)的: 在xml中定義, 適用于一些通用的動作.
  • 動態(tài)的: 由ShortcutManager發(fā)布, 可以根據(jù)用戶的行為或者偏好添加, 可以動態(tài)更新.

靜態(tài)使用

在應(yīng)用的Manifest中啟動Activity上添加meta-data

<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>

然后在res/xml/ 目錄下創(chuàng)建 shortcuts.xml文件, 里面包含靜態(tài)的shortcuts:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:enabled="true"
        android:icon="@mipmap/icon1"
        android:shortcutDisabledMessage="@string/static_message"
        android:shortcutId="static1"
        android:shortcutLongLabel="@string/static_long_label_1"
        android:shortcutShortLabel="@string/static_short_label_1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.githubly.shortcutsdemo.StaticTestActivity"
            android:targetPackage="com.githubly.shortcutsdemo" />
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@mipmap/icon2"
        android:shortcutDisabledMessage="@string/static_message"
        android:shortcutId="static2"
        android:shortcutLongLabel="@string/static_long_label_2"
        android:shortcutShortLabel="@string/static_short_label_2">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.githubly.shortcutsdemo.StaticTestActivity"
            android:targetPackage="com.githubly.shortcutsdemo" />
    </shortcut>

</shortcuts>

就這樣就可以簡單的創(chuàng)建了兩個靜態(tài)的Shortcut,targetClass 表示點擊快捷方式跳轉(zhuǎn)的頁面


靜態(tài)注冊

動態(tài)使用

動態(tài)的shortcuts可以在用戶使用app的過程中構(gòu)建, 更新, 或者刪除.
使用ShortcutManager可以對動態(tài)shortcuts完成下面幾種操作:

  • Publish發(fā)布: setDynamicShortcuts(), addDynamicShortcuts(List);
  • Update更新: updateShortcuts(List);
  • Remove刪除: removeDynamicShortcuts(List), removeAllDynamicShortcuts().

創(chuàng)建ShortcutInfo

private fun addShortcutWithIntent1(): ShortcutInfo? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        ShortcutInfo.Builder(this, "Dynamic1").apply {
            setShortLabel("動態(tài)快捷1")
            setLongLabel("DynamicShortcutLong1")
            setIcon(Icon.createWithResource(this@MainActivity, R.mipmap.icon3))
            setIntent(Intent().apply {
                action = Intent.ACTION_MAIN
                setClass(this@MainActivity, DynamicTestActivity::class.java)
                putExtra("info", "Dynamic shortcuts target class with intent1")
            })
        }.build()
    } else {
        null
    }
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
    val shortcutManager = getSystemService(ShortcutManager::class.java)

    val count = shortcutManager.maxShortcutCountPerActivity
    Log.e("count",count.toString())

    val list = mutableListOf<ShortcutInfo>()
    addShortcutWithIntent1()?.let {
        list.add(it)
    }
    /*addShortcutWithIntent2()?.let {
        list.add(it)
    }*/
    addShortcutWithIntents()?.let {
        list.add(it)
    }
    shortcutManager.dynamicShortcuts =list
}

多個Intent構(gòu)建back stack

動態(tài)的shortcut仍然可以用多個Intent來指定一個back stack, 那么打開目標Activity之后就可以返回到應(yīng)用中的指定界面而不是回到launcher:

private fun addShortcutWithIntents(): ShortcutInfo? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        ShortcutInfo.Builder(this, "Dynamic3").apply {
            setShortLabel("動態(tài)快捷3")
            setLongLabel("DynamicShortcutLong3")
            setIcon(Icon.createWithResource(this@MainActivity, R.mipmap.icon5))
            setIntents(arrayOf(
                    Intent().apply {
                        action = Intent.ACTION_MAIN
                        setClass(this@MainActivity, MainActivity::class.java)
                        flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
                    },
                    Intent().apply {
                        action = Intent.ACTION_MAIN
                        setClass(this@MainActivity, DynamicTestActivity::class.java)
                        flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
                        putExtra("info", "Dynamic shortcuts target class with intents")
                    }
            )
            )
        }.build()
    } else {
        null
    }
}

動態(tài)添加


動態(tài)添加

shortcut數(shù)量

val count = shortcutManager.maxShortcutCountPerActivity
Log.e("count",count.toString())

目前雖然說是 5 個,但實際最多只能添加 4 個(最多添加 4 個 Shortcuts 以保持在啟動器中顯示的樣式最佳)

如圖實際是靜態(tài)兩個加上動態(tài)三個實際上只顯示了四個


數(shù)量問題

疑問?

靜態(tài)使用 長按后顯示的是android:shortcutLongLabel的值,如果不設(shè)置android:shortcutLongLabel則顯示android:shortcutShortLabel

動態(tài)使用 長按后顯示的是setShortLabel的值

github地址 ShortcutsDemo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容