0

I know I can create a widget that that can be placed on the homescreen, but is there a possibility that when the user installs the app, only my standard launcher icon will start a certain activity. But when the user chooses so (by clicking a button for example in my app) another icon will be created on the home screen of the device that directly links to another activity? So by clicking that icon on the home screen another activity in my package will open?

Does anybody has a snippet if possible?

Thanks!

1 Answer 1

2

Thanks to this blog: http://viralpatel.net/blogs/android-install-uninstall-shortcut-example/

In the Manifest add the needed permissions:

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

add to your activity in the manifest where the shortcut is referring to:

   android:exported="true"

Then use the following methods to install/uninstall the shortcut:

 private void addShortcut() {
        //Adding shortcut for MainActivity 
        //on Home screen
        Intent shortcutIntent = new Intent(getApplicationContext(),
                MainActivity.class);

        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
                .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                        R.drawable.ic_launcher));

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


private void removeShortcut() {

        //Deleting shortcut for MainActivity 
        //on Home screen
        Intent shortcutIntent = new Intent(getApplicationContext(),
                MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
                .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");

        addIntent
                .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }

To add an activity to the shortcuts manu simply add this intent filter to your activity in your manifest:

<intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.