พาส่อง Android App Shortcuts สำหรับนักพัฒนา
ตั้งแต่ Android 7.1 Nougat (API level 25) Android ได้เพิ่มฟีเจอร์ App Shortcuts เข้ามา ซึ่งเจ้า App Shortcuts เนี่ยก็เป็นเสมือนทางลัดในการเข้าถึงฟังก์ชั่นของแอป
โดยวิธีการใช้งานก็เพียงกดค้างที่ไอคอนแอป จะมี Popup menu โผล่ขึ้นมา ถ้าแอปนั้นๆ รองรับและ Android ของคุณเป็นเวอร์ชั่น 7.1 Nougat อีกข้อคือ Launcher ต้องรองรับอีกด้วย ใครใช้ Android 7.1 ขึ้นไปก็ลองกดค้างที่ไอคอนแอปดูครับ ซึ่งแอปของกูเกิลน่าจะรองรับหมดแล้ว เช่น Google Map
เอาล่ะ! มาเริ่มส่อง App Shortcuts กัน
เจ้า App Shortcuts เนี่ยมีสองแบบคือ Static Shortcuts และ Dynamic Shortcuts โดยแต่ละแอปจะมี App Shortcuts ได้มากสุด 5 shortcut
เริ่มจาก Static Shortcuts ก่อนละกัน
สร้าง resource file ที่ res/xml/shortcuts.xml
สร้าง shortcut ด้วย tag <shortcut></shortcuts> แบบด้านล่าง
Attributes configure
- android:shortcutId : ก็คือไอดีของ shortcut นั่นแหละ ค่าเป็น string ไม่สามารถใช้ @string ใน resource
- android:icon : ไอคอนของ shortcut นั้นๆ
- android:shortcutLongLabel : ชื่อของ shortcut แสดงที่ Popup menu ค่าเป็น resource string (เช่น @string/shortcut_long_label)
- android:shortcutShortLabel : ชื่อของ shortcut จะแสดงเมื่อเราลาก shortcut ออกมาวางที่หน้าจอเหมือนไอคอนแอปอีกอันนึง
- android:enable : เปิดใช้งาน shortcut ไหม(true = แสดง/ false = ไม่แสดง)
android:enabled=”false” มีค่าเท่ากับลบ shortcut นั้นๆ ออกจาก resource - android:shortcutDisabledMessage : ข้อความที่แสดงเมื่อ shortcut ที่ enable = false หรือถูกลบออกไปจาก resource shortcuts แล้วถูกเรียกใช้
- <intent></intent> : tag นี้ก็คือ คำสั่ง intent ที่จะให้ทำงานจากการเลือก shortcut (ในที่นี้คือให้เปิด Activity AddContactActivity)
จากนั้นเพิ่ม <meta-data> ด้านล่างใน activity ที่เป็น launcher ของ app ที่ไฟล์ AndroidManifest.xml เพื่อระบุ shortcuts resource ไห้กับแอป
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts"/>ได้เป็นแบบนี้
เท่านี้แอปเราก็จะมี Shortcuts แล้ว
Get JingjoeH’s stories in your inbox
Join Medium for free to get updates from this writer.
มาต่อกันที่ Dynamic Shortcuts
เราจะใช้งาน Dynamic Shortcuts ด้วย ShortcutManager
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);สร้าง Dynamic Shortcut ด้วย ShortcutInfo.Builder()
ShortcutInfo info = new ShortcutInfo.Builder(mContext, "shortID")
.setShortLabel("Short Label")
.setLongLabel("Long Label")
.setIcon(icon)
.setIntent(intent)
.build();เพิ่ม Dynamic Shortcut ด้วยคำสั่ง addDynamicShortcuts(List<ShortcutInfo>)
โดยรับค่าเป็นลิสต์ของ ShortcutInfo
shortcutManager.addDynamicShortcuts(List<ShortcutInfo> infoList);หรือจะเซ็ต Dynamic Shortcuts ใหม่ด้วยคำสั่ง
shortcutManager.setDynamicShortcuts(List<ShortcutInfo> infoList)แต่ต้องระวังไว้ว่า App Shortcuts มีได้มากสุด 5 อัน(รวมแบบ static และ dynamic แล้ว) ก่อนจะเพิ่มหรือเซ็ตต้องเช็คก่อนโดยเราสามารถดึง List<ShortcutInfo> ได้จาก
shortcutManager.getManifestShortcuts() // แบบ static
shortcutManager.getDynamicShortcuts() // แบบ dynamicลบ Shortcut ได้ด้วยคำสั่ง รับค่าเป็นลิสต์ของ shortcutId
shortcutManager.removeDynamicShortcuts(List<String> shortcutIds)หรือจะลบ Dynamic shortcuts ทั้งหมดเลยก็ได้
shortcutManager.removeAllDynamicShortcuts()Disable ด้วยคำสั่ง
shortcutManager.disableShortcuts(List<String> shortcutIds)Update shortcuts ที่มีอยู่ด้วย โดยจะ update shortcut ที่มี id เหมือนกัน
shortcutManager.updateShortcuts(List<ShortcutInfo> infoList)คำสั่งก็จะประมาณนี้สำหรับ Dynamic Shortcuts ดูเพิ่มเติมได้ที่ ShortcutManager
โดยคำสั่งจาก ShortcutManager ไม่สามารถใช้กับ Static shortcut ได้นะครับ
วิธีเช็คประเภทของ shortcut
shortcutInfo.isDynamic(); // เช็คว่าเป็น Dynamic shortcut ?
shortcutInfo.isDeclaredInManifest(); // เช็คว่าเป็น Static shortcut ?หลังจากการส่อง App Shortcuts ของผมพบว่า ถึงแม้ App Shortcuts สามารถมีได้มากสุด 5 อัน แต่เวลาแสดงเป็น popup menu จริงๆกลับแสดงมากสุดเพียง 4 อันเท่านั้น
ในบทความนี้ก็คงจะพาส่องเจ้า Android App Shortcuts กันประมาณนี้ก็หวังว่าจะมีประโยชน์นะครับ











