Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating multible shortcuts for one Activity via Intent

I have an Activity that should display different content based on the shortcut the user pressed on the home screen.

My Code looks like this:

AndroidManifest.xml

    <activity
        android:name=".activities.ShortcutActivity_"
        android:parentActivityName=".activities.MainActivity_"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Method for creating the shortcut:

private void createShortcut(String label){
    Intent shortcut = new Intent(getApplicationContext(), ShortcutActivity_.class);
    shortcut.setAction(Intent.ACTION_MAIN);

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.mipmap.ic_launcher));
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    intent.putExtra("duplicate", false);
    getApplicationContext().sendBroadcast(intent);
}

I'm calling the method like this:

createShortcut(ShortcutActivity_.class, "Shortcut 1");
createShortcut(ShortcutActivity_.class, "Shortcut 2");

In my Activity I want to check for the Labels and show different content for each shortcut but it doesn't work. Only one shortcut will be created.

How can I build a dynamic Activity that can display different content based on the pressed shortcut?

thanks for your help!

like image 373
blk0ut Avatar asked Dec 13 '25 07:12

blk0ut


1 Answers

The following code worked in my case.

private void createShortcut(Class c, String label, int id) {
    Intent shortcut = new Intent(getApplicationContext(), c);
    shortcut.putExtra(SHORTCUT_ID, id);
    shortcut.putExtra("duplicate", false);
    shortcut.setAction(Intent.ACTION_DEFAULT);

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.mipmap.ic_launcher));
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    intent.putExtra("duplicate", false);
    getApplicationContext().sendBroadcast(intent);
}

Invoke the method like this in your activity:

createShortcut(ShortcutActivity.class, "Shortcut 2", 2);

And in your ShortcutActivity get the Intent like this and respond to it.

Intent intent = getIntent();
    if (intent == null)
        return;

    if (intent.getIntExtra(MainActivity.SHORTCUT_ID, 0) == 1){
        textView2.setText("Shortcut 1");
    }
    if (intent.getIntExtra(MainActivity.SHORTCUT_ID, 0) == 2){
        textView2.setText("Shortcut 2");
    }
like image 178
blk0ut Avatar answered Dec 15 '25 22:12

blk0ut