Android create shortcuts on the home screen

AndroidShortcutHomescreen

Android Problem Overview


What I want to do is:

  1. I'm inside an activity, there are 2 buttons. If I click the first one a shortcut is created in my home screen. The shortcut open an html page that has been previously downloaded, so I want it to use the default browser but I don't want to use internet cause I already have the page.

2)The second button create another shortcut that starts an activity. And i want to pass to the activity some extra arguments (As strings for example)...........

Are those things possible? I found some link and some similar questions like https://stackoverflow.com/questions/5676090/android-is-there-a-programming-way-to-create-a-web-shortcut-on-home-screen

They seem to be the answer to my question but someone told me that this code is not gonna work on all devices and that is deprecated and that what i want to do is not possible.......

> This technique is not recommended. This is an internal implementation, not part of the Android SDK. It will not work on all home screen implementations. It may not work on all past versions of Android. It may not work in future versions of Android, as Google is not obligated to maintain internal undocumented interfaces. Please do not use this

What means internal implementation? Is that code trustable or not.....help me pls.....

Android Solutions


Solution 1 - Android

The example code uses undocumented interfaces (permission and intent) to install a shortcut. As "someone" told you, this may not work on all phones, and may break in future Android releases. Don't do it.

The correct way is to listen for a shortcut request from the home screen-- with an intent filter like so in your manifest:

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result.

// create shortcut if requested
ShortcutIconResource icon =
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

Intent intent = new Intent();

Intent launchIntent = new Intent(this,ActivityToLaunch.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

setResult(RESULT_OK, intent);

Solution 2 - Android

I have developed one method below for creating the shortcut icon on android Homescreen [Tested on my own app]. Just call it.

private void ShortcutIcon(){
	
	Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
	shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	
	Intent addIntent = new Intent();
	addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
	addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
	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);
}

Don't forget to change your activity name, icon resource & permission.

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

Happy coding !!!

Edit:

For Duplicate Issue, First Option is to add below line in the code otherwise it creates new one every time.

addIntent.putExtra("duplicate", false);

Second Option is to First uninstall The App Shortcut Icon and then Install It Again If the First Option Didn't Work.

Solution 3 - Android

The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect since android oreo. LINK

If you want to support all android versions, especially android 8.0 or oreo and newer, use the code below to create shortcut:

public static void addShortcutToHomeScreen(Context context)
{
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
    {
        ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                .setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                .setShortLabel("Test")
                .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
                .build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
    }
    else
    {
        // Shortcut is not supported by your launcher
    }
}

Add permission in manifest file:

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

Solution 4 - Android

Starting from Android O, this is the way to create a shortcut:

if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
    final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

    ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, shortcutId)
            .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
            .setShortLabel(label)
            .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN))
            .build();
    shortcutManager.requestPinShortcut(pinShortcutInfo, null);
}

It has a lot of limitations, sadly:

  1. Requires the user to accept adding it.
  2. Can't be added/removed in the background.
  3. Won't be removed if the targeted app is removed. Only the one which created it.
  4. Can't create the icon based on a resource of the targeted app, except if it's of the current app. You can do it from a bitmap though.

For pre-Android O, you can use ShortcutManagerCompat to create new shortcuts too, without any of those limitations.

Solution 5 - Android

I improved a little bit a solution above. Now it saves in preferences whether a shortcut was already added and doesn't add it in new launches of an app if user deleted it. This also saves a little bit time, since the code to add an existing shortcut doesn't run anymore.

final static public String PREFS_NAME = "PREFS_NAME";
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";


// Creates shortcut on Android widget screen
private void createShortcutIcon(){
	
	// Checking if ShortCut was already added
	SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
	boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
	if (shortCutWasAlreadyAdded) return;
	
    Intent shortcutIntent = new Intent(getApplicationContext(), IntroActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YourAppName");
    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);
    
    // Remembering that ShortCut was already added
	SharedPreferences.Editor editor = sharedPreferences.edit();
	editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
	editor.commit();
}

Solution 6 - Android

@Siddiq Abu Bakkar Answer works. But in order to prevent creating shortcut every time app launches use shared Preferences.

final String PREF_FIRST_START = "AppFirstLaunch";
 SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0);
    if(settings.getBoolean("AppFirstLaunch", true)){

        // record the fact that the app has been started at least once
        settings.edit().putBoolean("AppFirstLaunch", false).commit();
        ShortcutIcon();

    }

Solution 7 - Android

Since API level 26, using com.android.launcher.action.INSTALL_SHORTCUT is deprecated. The new way of creating shortcuts are using ShortcutManager.

It mentions that there are 3 kinds of shortcuts, static, dynamic and pinned. Based on your requirements, you can choose to create them.

Solution 8 - Android

To add a shortcut to the home screen use this code.

public void addShortcutToHomeScreen(Context context) {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                    .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                    .setShortLabel("Label Goes Here")
                    .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher_shortcut))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
        } else {
            Toast.makeText(context, R.string.no_shortcut, Toast.LENGTH_SHORT).show();
        }
    }

and no extra permission need !!!

Solution 9 - Android

final Intent shortcutIntent = new Intent(this, SomeActivity.class);

final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);

Solution 10 - Android

public static void addShortcutToHomeScreen(Context context)
{
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
    {
        ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                .setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                .setShortLabel("Test")
                .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
                .build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
    }
    else
    {
        // Shortcut is not supported by your launcher
    }
}

I've used it, but some devices on the home screen adds 2 icons. I did not understand??

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSgotenksView Question on Stackoverflow
Solution 1 - AndroidantlersoftView Answer on Stackoverflow
Solution 2 - AndroidSiddiq Abu BakkarView Answer on Stackoverflow
Solution 3 - AndroidRunView Answer on Stackoverflow
Solution 4 - Androidandroid developerView Answer on Stackoverflow
Solution 5 - AndroidDenis KutlubaevView Answer on Stackoverflow
Solution 6 - AndroidKrishnadas PCView Answer on Stackoverflow
Solution 7 - AndroidnoobView Answer on Stackoverflow
Solution 8 - AndroidKundan SinghView Answer on Stackoverflow
Solution 9 - AndroidPiush KumarView Answer on Stackoverflow
Solution 10 - Androidselçuk doğanView Answer on Stackoverflow