How to create a shortcut of an app on a SPECIFIC launcher app?

喜你入骨 提交于 2020-01-25 10:54:28

问题


Background

I already know how to put a shortcut to an app globally:

Intent shortcutIntent=new Intent(); shortcutIntent.setComponent(new ComponentName(packageName,fullPathToActivity));

final Intent putShortCutIntent=new Intent();
putShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
//... <=preparing putShortcutIntent with some customizations (title, icon,...)
putShortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(putShortcutIntent);

This works well. What I do wish to do is to be able to put the shortcut only to a specific launcher app (in fact it's the default launcher app, but that's not the problem).

The problem

For some reason, instead of putting the shortcut on the launcher app i've chosen (in this case "nova launcher") , I end up getting the shortcut created on the touchwiz launcher alone.

What I've tried

the obvious solutions were:

putShortcutIntent.setClassName(launcherPackageName,broadcastReceiverClassName);

or

putShortcutIntent.setPackage(launcherPackageName);

none worked, even though i've seen the values fine during debug mode (the parameters were legit).

The weird thing is that if I use this, it puts the shortcut only on the touchwiz launcher...

The question

Why does it occur?

How come I can't choose where to send the broadcast to?

is there a better way to achieve this?


EDIT: here's how I get the default broadcastReceiver that can handle the shortcut creation intent:

final ResolveInfo defaultActivityForLauncherIntent=... //here i return the correct app that is the default launcher
Intent intentForAddingShortcutOnDesktop=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//<= here I prepare the intent of creating a shortcut
final List<ResolveInfo> broadcastReceiversResolveInfos=context.getPackageManager().queryBroadcastReceivers(intentForAddingShortcutOnDesktop,0);
for(final ResolveInfo resolveInfo : broadcastReceiversResolveInfos)
  if(resolveInfo.activityInfo.packageName.equals(defaultActivityForLauncherIntent.activityInfo.packageName))
    {
    intentForAddingShortcutOnDesktop.setComponent(new ComponentName(//
        resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name));
    break;
    }
context.sendBroadcast(intentForAddingShortcutOnDesktop); 

it seems part of the confusion was that the play store was set to create new shortcuts.

This, and the fact i forgot to query the broadcastReceivers instead of activities...

来源:https://stackoverflow.com/questions/21394477/how-to-create-a-shortcut-of-an-app-on-a-specific-launcher-app

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