Täg Täg - 1 month ago 15
Java Question

Installing shortcut wihout duplicating native handly created one

I want to add a shortcut to my application, but I cannot manage not to duplicate native handly created one (by using drag and drop on application Icon in Application Menu to Home Screen for example).

Here is my code:

public void addShortcut(Context context)
{
this.manageShortcutAction(context, "com.android.launcher.action.INSTALL_SHORTCUT");
}

public void deleteShortcut(Context context)
{
this.manageShortcutAction(context, "com.android.launcher.action.UNINSTALL_SHORTCUT");
}

private void manageShortcutAction(Context context, String intentAction)
{
Context applicationContext = context.getApplicationContext();
Intent shortcut = new Intent(intentAction);
ApplicationInfo appInfo = applicationContext.getApplicationInfo();
PackageManager packageManager= applicationContext.getPackageManager();
String applicationName = (String) packageManager.getApplicationLabel(appInfo);

shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, applicationName); // Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, packageManager.getLaunchIntentForPackage(appInfo.packageName));// Setup activity should be shortcut object
shortcut.putExtra("duplicate", false); // Just create once
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(applicationContext, appInfo.icon));// Set shortcut icon

applicationContext.sendBroadcast(shortcut);
}





And my manifest required permissions:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />





By the way, I had overwritten the Application code which is now
MainApplication
extending
Application
.
I have already tried to create a component to create the
Intent.EXTRA_SHORTCUT_INTENT
, without the expecting result.

If anyone has an idea...

Answer

I found out that in order to prevent the app from creating a duplicate of the shortcut created at app installation (or by copying from the menu), I have to create one with the same parameters of the existing one - using the same shortcut name is not enough.

After a lot of testing and thanks to the logcat, I've been able to create an exact replica as follows:

private void installShortcut() {
    final Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    final Intent intent = new Intent();
    intent.putExtra("duplicate", false);
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
}

The difference with other answers I found on SO is in these 3 lines:

    shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

which I figured out by looking at the log entry printed when manually launching the app using the system created shortcut

Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.myapp/.activities.main.MainActivity bnds=[365,73][475,191] } from pid 279
Comments