9

I have seen ways to make shortcuts, but I need to find a way to get a list of shortcuts installed on the phone.

I want my user to be able to select one of his/her shortcuts and launch it from my application. Is there a way to do this (an API) or will I need a reflection method to call a system service?

SemperGumbee
  • 474
  • 1
  • 8
  • 19

4 Answers4

5

Here is how it is done in the Launcher...

Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
List<ResolveInfo> shortcuts = getPackageManager().queryIntentActivities(shortcutsIntent, 0);
Jonathan
  • 1,078
  • 11
  • 9
  • 1
    Tried this.. it doesn't work. it just gave back some android preset shortcuts like browser, contacts etc.. – Guy Sep 05 '12 at 06:50
  • This solution works to get the retro shortcuts. To get info from the list of ResolveInfo objects, see the following: for (ResolveInfo shortcut: shortcuts){ Log.d("SC", ""+shortcut.activityInfo.applicationInfo.loadLabel(getPackageManager())); Log.d("SC", shortcut.activityInfo.packageName); Log.d("SC", ""+shortcut.loadLabel(getPackageManager())); } – Michael Kern Oct 30 '18 at 17:32
2

The shortcuts are private to Launcher. There is no API, and anything you try to do will be very fragile as different launcher implementations (and versions) will have different storage structures.

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • I have seen a few apps come up with a way to do this without being a launcher. I know it will involve getting the intents in the category alternative. But what if the intent to launch said shortcut has nested options? For example, you choose the setting shortcut and that launches a bunch of other options for which setting you want a shortcut for. I know there is a way and I guess reflection was a bad word to use. I just need to figure out a way to get these shortcuts in the category, get the intents and launch them when the user does some action in my application :/ – SemperGumbee Jun 01 '11 at 06:58
  • @SemperGumbee: "I have seen a few apps come up with a way to do this without being a launcher" -- name any. "I know it will involve getting the intents in the category alternative." -- AFAIK you are mistaken. Finding activities that support the `LAUNCHER` category is what a launcher does to find launchable activities. The `ALTERNATIVE` category is not much used, nowadays, and AFAIK has nothing to do with shortcuts. – CommonsWare Jun 01 '11 at 11:16
  • @CommonsWare Thanks for the info. One app I have found that can do this is SwipePad. Now, I don't know what the developer did to get shortcuts working. For application launching in my app, I just populate a list of the apps in the CATEGOTY_LAUNCHER category and ACTION_MAIN and get the package name and store them in preferences for launching later. Can I maybe refine my Intent here to find the shortcuts? – SemperGumbee Jun 01 '11 at 17:08
0

In addition to Jonathan,

Each app can do shortcuts. Each shortcut specified in app's manifest. So you can get shortcuts list (this method in activity):
Kotlin

fun printShortcuts() {
    val shortcutIntent = Intent(Intent.ACTION_CREATE_SHORTCUT)
    val shortcuts = packageManager.queryIntentActivities(shortcutIntent, 0)
    shortcuts.forEach {
        println("name = ${it.activityInfo.name}, label = ${it.loadLabel(packageManager)}")
    }
}

It will print something like:

I/System.out: name = com.whatsapp.camera.CameraActivity, label = WhatsApp Camera
I/System.out: name = com.android.contacts.ContactShortcut, label = Contact
I/System.out: name = alias.DialShortcut, label = Direct dial
I/System.out: name = alias.MessageShortcut, label = Direct message
Evgenii Vorobei
  • 1,457
  • 18
  • 20
-2

I'm trying to do the same and there a lot of things not clear about this topic, at least not clear to me...........An example is Openapp market that create shortcuts everytime you "download" an app, but the shortcuts it is actually only a link to an html page. Anyway i have 2 android phones and in the firstone is working in the second one is not creating any shortcuts.......

in may app i do the following:

 Intent shortcutIntent = new Intent(this,FinestraPrincipaleActivity.class);
 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  shortcutIntent.putExtra("someParameter", "HelloWorld");

 Intent addIntent = new Intent();
 addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
 addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
  addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  
 Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));

addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
 this.sendBroadcast(addIntent);

But someone told me that the is wrong but i didn't find anyother way to create shortcuts....

pengwang
  • 19,536
  • 34
  • 119
  • 168
Sgotenks
  • 1,723
  • 4
  • 20
  • 34
  • It looks like you're just making a shortcut and then sending the broadcast to the launcher. I need to find the shortcuts already available. They might be different for each device, so I just want a way to be able to get all of the shortcuts that are already available and have my user select one to launch later. – SemperGumbee Jun 15 '11 at 00:04