How to get available “app shortcuts” of a specific app?

前端 未结 2 367
生来不讨喜
生来不讨喜 2021-02-04 09:33

Background

Starting from API 25 of Android, apps can offer extra shortcuts in the launcher, by long clicking on them:

The problem

Thing is,

相关标签:
2条回答
  • 2021-02-04 10:03

    You need to make yourself the launcher app. After that you can query the packagemanager to get the shortcutinfo for a particular package:

    fun getShortcutFromApp(packageName: String): List<Shortcut> {
        val shortcutQuery = LauncherApps.ShortcutQuery()
        shortcutQuery.setQueryFlags(FLAG_MATCH_DYNAMIC or FLAG_MATCH_MANIFEST or FLAG_MATCH_PINNED)
        shortcutQuery.setPackage(packageName)
        return try {
            launcherApps.getShortcuts(shortcutQuery, Process.myUserHandle())
                    .map { Shortcut(it.id, it.`package`, it.shortLabel.toString(), it) }
        } catch (e: SecurityException) {
            Collections.emptyList()
        }
    

    }

    A full implementation of this can be found here:

    https://android.jlelse.eu/nhandling-shortcuts-when-building-an-android-launcher-5908d0bb50d2

    Github link to project:

    https://github.com/nongdenchet/Shortcuts

    LauncherApps is a class provided by the Android framework:

    https://developer.android.com/reference/android/content/pm/LauncherApps.html

    "Class for retrieving a list of launchable activities for the current user and any associated managed profiles that are visible to the current user, which can be retrieved with getProfiles(). This is mainly for use by launchers. Apps can be queried for each user profile. Since the PackageManager will not deliver package broadcasts for other profiles, you can register for package changes here."

    0 讨论(0)
  • 2021-02-04 10:15

    Great Question this was what i was looking for and I get one that might be useful

    (Its all about Intent)

    https://github.com/googlesamples/android-AppShortcuts

    Note: If your app is static its simple to implement.

    Updated:::

    Here is a link that will show you the difference of dynamic or static shortcuts and its implementation if you like it please up vote.

    https://www.novoda.com/blog/exploring-android-nougat-7-1-app-shortcuts/

    0 讨论(0)
提交回复
热议问题