[Kotlin] Android 建立應用程式捷徑

首頁固定捷徑 Pinned shortcuts

    
fun addDynamicAppShortcut(context: Context) {
    // 觸發後要開啟的 intent
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"))

    val shortcut = ShortcutInfoCompat.Builder(context, "shortcut_id")
        .setShortLabel("應用程式捷徑")
        .setLongLabel("這是一個動態建立的應用程式捷徑")
        .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher_foreground))
        .setIntent(intent)
        .build()
    
    // 建立首頁固定捷徑
    ShortcutManagerCompat.requestPinShortcut(context, shortcut, null)
}

    


應用程式捷徑 App shortcuts

應用程式捷徑和首頁固定捷徑只差在最後一行
    
fun addDynamicAppShortcut(context: Context) {
    // 觸發後要開啟的 intent
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"))

    val shortcut = ShortcutInfoCompat.Builder(context, "shortcut_id")
        .setShortLabel("應用程式捷徑")
        .setLongLabel("這是一個動態建立的應用程式捷徑")
        .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher_foreground))
        .setIntent(intent)
        .build()
    
    // 建立應用程式捷徑
    ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
}

    


並且 應用程式捷徑 還可以直接拖出來變成 首頁固定捷徑:


參考資料:
Android Developers - App shortcuts overview

留言