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

前端 未结 2 374
生来不讨喜
生来不讨喜 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 {
        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."

提交回复
热议问题