Suppose, I have some Android app which helps users to install some other apps. Is there any way to create a shortcut of this apps on home screen? Can I also specify the position of these shortcuts?
Asked
Active
Viewed 5,606 times
2
-
I don't understand; What do you mean shortcuts? So you want to create app icons/widgets for the installed apps? – LotusUNSW Nov 14 '13 at 12:44
-
@LotusUNSW Yes, I want to create app icons for the installed apps – Mykhailo Granik Nov 14 '13 at 12:48
3 Answers
1
Try this :
public void createShortCut() {
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext, R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent("com.whatsapp"));
sendBroadcast(shortcutintent);
}

Pratik Butani
- 60,504
- 58
- 273
- 437

kapil thadani
- 1,425
- 14
- 26
-
thanx for the code but is there a way to check if a similar short cut already exists before creating a newer one ? – kinsley kajiva Feb 01 '17 at 06:21
-
-
-1
A bit improved version: we are checking if the shortcut has been already created and must not be created if user remove it from the screen
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";
@Override
public void onStart(){
super.onStart();
// Checking if ShortCut was already added
sharedPreferences = getPreferences(MODE_PRIVATE);
boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
if (!shortCutWasAlreadyAdded) createShortcutIcon();
} // end onStart
// Creates shortcut on Android widget screen
private void createShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), Splash.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, getString(R.string.app_name));
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();
objPublicDelegate.showToast(getString(R.string.app_name)+ " shortcut created on screen.");
} // end createShortcutIcon

Vinod Joshi
- 7,696
- 1
- 50
- 51
-
Thanks. shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Without these two lines, sometimes app is crashing saying "TransactionTooLargeException". To avoid this exception, its mandatory to add these lines. – Ramesh Bandari May 16 '17 at 06:18