How to get available "app shortcuts" of a specific app?
Asked Answered
V

2

14

Background

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

enter image description here

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?

Vyky answered 16/1, 2018 at 9:18 Comment(0)
A
10

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

Appurtenance answered 28/1, 2018 at 16:26 Comment(5)
It won't work in case my app isn't the launcher app? Also, what's the Shortcut class? And launcherApps ? Your code is missing important things to run. And you haven't answered the question I wrote: is it possible to create a pinned shortcut out of it?Vyky
According to the docs that is correct. The rest of the code is in the github project. Didn't realize you had two questions in one. :)Appurtenance
Please put the minimal code for this to work. Isn't there any workaround to make it work without being the launcher?Vyky
Please see the following for that question: developer.android.com/reference/android/content/pm/…Appurtenance
That's too bad. Please put full, minimal code though.Vyky
J
1

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/

Jezebel answered 3/2, 2018 at 5:26 Comment(3)
What do you mean "If your app is static" ? Also, the question is how to query them of other apps. And, I can't import it for some reason: github.com/googlesamples/android-AppShortcuts/issues/16 . I just want to see full, minimal implementation of how to do it here.Vyky
please see my update answer its a simple implementation exampleJezebel
Again, I didn't ask how to create them for the launcher. I asked how to query them about any app. For example, Google-Calendar has "new reminder" and "new event". The question was that if I set the input of Google Calendar package name ("com.google.android.calendar") , the output of the query will be those 2 app-shortcuts.Vyky

© 2022 - 2024 — McMap. All rights reserved.