Created
April 10, 2014 21:03
-
-
Save zakelfassi/10423203 to your computer and use it in GitHub Desktop.
Create android app shortcut programatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} |
This code doesn't work for xiaomi devices
thanx for the code but is there a way to check if a similar short cut already exists before creating a newer one ?
<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
thanks for the code, I have a question, you know there are launchers and some apps like 'all in one gestures' it provides an interface to select shortcut and launch it with gesture so how can I have my app to launch shortcut and make user select which shortcut he wants it to launch???