App shortcut¶
Create pinned shortcuts¶
On Android 8.0 (API level 26) and higher, you can create pinned shortcuts. Unlike static and dynamic shortcuts, pinned shortcuts appear in supported launchers as separate icons. Figure 1 shows the distinction between these two types of shortcuts.
Note
Note: When you attempt to pin a shortcut onto a supported launcher, the user receives a confirmation dialog asking their permission to pin the shortcut. If the user doesn't allow the shortcut to be pinned, the launcher cancels the request.
The following example shows you how to create an application shortcut.
createPinnedShortcut("OnlyId") {
return@createPinnedShortcut Intent(context, MainActivity::class.java)
}
Set resultIntent for shortcut¶
Set resultIntent by shortcutResultIntent
.
createPinnedShortcut(
shortcutId = "OnlyId",
shortcutResultIntent = { intent ->
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}
) {
return@createPinnedShortcut Intent(context, MainActivity::class.java).apply {
action = Intent.ACTION_VIEW
}
}
Create dynamic shortcuts¶
Using DynamicShortcuts
to create dynamic shortcuts.
val shortcut = ShortcutInfoCompat.Builder(context, "OnlyId")
.setShortLabel("Website")
.setLongLabel("Open the website")
.setIcon(IconCompat.createWithResource(
context, R.drawable.ic_launcher_foreground))
.setIntent(Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.mysite.example.com/")))
.build()
addDynamicShortcuts(listOf(shortcut))