Past few days I have been trying to figure out how to start app shortcuts added in Android API 25 and I have been somewhat successful. Starting some shortcuts like Google Play Music's work without problem I'm guessing because these shortcuts are starting activities with action.MAIN and category.LAUNCHER or exported, but other activities throw "java.lang.SecurityException: Permission Denial: starting Intent" which seems reasonable as well. This seems dooable since apps like Pixel Launcher, Sesame Shortcuts etc. can do this, there must be some special intent flag or something. I even tried catching shortcut intent inside my app and replicating it but no success
Here is example of how I do this for Google Chrome's "New Tab" shortcut:
val componentName = ComponentName("com.android.chrome", "org.chromium.chrome.browser.LauncherShortcutActivity")
val intent = Intent(action)
intent.component = componentName
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME)
startActivity(intent)
I got these values from activityInfo metaData which can be fetched with PackageManager
Here's the code to list all available shortcuts and then launch whatever the first shortcut is (just as an example):
This code assumes that you have a variable named
context
of typeContext
and a variable namedpackageName
of typeString
from somewhere.Credits: This is all based on Quan Vu's work on this topic, his article here and the corresponding repository here.