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

≡放荡痞女 提交于 2019-12-03 09:29:01

问题


Background

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

The problem

Thing is, all I've found is how your app can offer those shortcuts to the launcher, but I can't find out how the launcher gets the list of them.

Since it's a rather new API, and most users and developers don't even use it, I can't find much information about it, especially because I want to search of the "other side" of the API usage.

What I've tried

I tried reading the docs (here, for example). I don't see it being mentioned. Only the part of other apps is mentioned, but not of the receiver app (the launcher).

The questions

Given a package name of an app, how can I get a list of all of its "app shortcuts" using the new API?

Is it possible to use it in order to request to create a Pinned Shortcut out of one of them?


回答1:


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




回答2:


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/



来源:https://stackoverflow.com/questions/48277743/how-to-get-available-app-shortcuts-of-a-specific-app

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