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

自闭症网瘾萝莉.ら 提交于 2019-12-03 01:11:51

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."

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!