Android App Shortcuts使用說明(長按快捷方式)
Shortcut
其中App Shortcuts是指在桌面長按app圖示而出現的快捷方式, 可以為你的app的關鍵功能新增更快速的入口而不用先開啟app,點選快捷方式可以訪問應用功能, 並且這種快捷方式也可以被拖拽到桌面單獨放置, 變成單獨的桌面快捷方式.
典型應用
支付寶&騰訊新聞(每次雖然國內跟進的晚,但是阿里騰訊也算是最早跟進的一批了)

支付寶

騰訊新聞
使用方式
兩種方式使用
- 靜態的: 在xml中定義, 適用於一些通用的動作.
- 動態的: 由ShortcutManager釋出, 可以根據使用者的行為或者偏好新增, 可以動態更新.
靜態使用
在應用的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/ 目錄下建立 shortcuts.xml 檔案, 裡面包含靜態的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>
就這樣就可以簡單的建立了兩個靜態的Shortcut,targetClass 表示點選快捷方式跳轉的頁面

靜態註冊
動態使用
動態的shortcuts可以在使用者使用app的過程中構建, 更新, 或者刪除.
使用ShortcutManager可以對動態shortcuts完成下面幾種操作:
- Publish釋出: setDynamicShortcuts(), addDynamicShortcuts(List);
- Update更新: updateShortcuts(List);
- Remove刪除: removeDynamicShortcuts(List), removeAllDynamicShortcuts().
建立ShortcutInfo
private fun addShortcutWithIntent1(): ShortcutInfo? { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutInfo.Builder(this, "Dynamic1").apply { setShortLabel("動態快捷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構建back stack
動態的shortcut仍然可以用多個Intent來指定一個back stack, 那麼開啟目標Activity之後就可以返回到應用中的指定介面而不是回到launcher:
private fun addShortcutWithIntents(): ShortcutInfo? { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { ShortcutInfo.Builder(this, "Dynamic3").apply { setShortLabel("動態快捷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 } }
動態新增

動態新增
shortcut數量
val count = shortcutManager.maxShortcutCountPerActivity Log.e("count",count.toString())
目前雖然說是 5 個,但實際最多隻能新增 4 個(最多新增 4 個 Shortcuts 以保持在啟動器中顯示的樣式最佳)
如圖實際是靜態兩個加上動態三個實際上只顯示了四個

數量問題
疑問?
靜態使用 長按後顯示的是 android:shortcutLongLabel 的值,如果不設定 android:shortcutLongLabel 則顯示 android:shortcutShortLabel
動態使用 長按後顯示的是 setShortLabel 的值