Skip to content

Instantly share code, notes, and snippets.

@zakelfassi
Created April 10, 2014 21:03
Show Gist options
  • Save zakelfassi/10423203 to your computer and use it in GitHub Desktop.
Save zakelfassi/10423203 to your computer and use it in GitHub Desktop.
Create android app shortcut programatically
// Note that a shortcut is created automagically if the app is installed via Play store.
// Change "APP_NAME" by your app name. *MrObvious*
/*Manifest file - add this */
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
/* MainActivity.java */
public class MainActivity ... {
...
private SharedPreferences appSettings;
...
protected void onCreate(Bundle savedInstanceState) {
...
...
appSettings = getSharedPreferences("APP_NAME", MODE_PRIVATE);
// Make sure you only run addShortcut() once, not to create duplicate shortcuts.
if(!appSettings.getBoolean("shortcut", false)) {
addShortcut();
}
}
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, "APP_NAME");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
SharedPreferences.Editor prefEditor = appSettings.edit();
prefEditor.putBoolean("shortcut", true);
prefEditor.commit();
}
}
@kinsleykajiva
Copy link

thanx for the code but is there a way to check if a similar short cut already exists before creating a newer one ?

@wypk
Copy link

wypk commented May 16, 2018

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

Permissions are needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment