Wednesday, September 10, 2014

Create Application Shortcut on Homescreen in Android

//Maintain a SharedPreference variable to check isFirstLaunch OR  not
 private void addShortcut() {
    if (checkfor isFirstlaunchOrNot in your sharedprefs) {
      Intent removeIntent = new Intent();
      removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
      getApplicationContext().sendBroadcast(removeIntent);
      Intent shortcutIntent = new Intent(getApplicationContext(), Splash.class);
      shortcutIntent.setAction(Intent.ACTION_MAIN);

      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);
//set FirstLaunch to false in your sharedprefs
    }
  }