Skip to content

Instantly share code, notes, and snippets.

@eufelipe
Created July 24, 2017 18:19
Show Gist options
  • Save eufelipe/35737c8f6a180198953a348cba8a3baa to your computer and use it in GitHub Desktop.
Save eufelipe/35737c8f6a180198953a348cba8a3baa to your computer and use it in GitHub Desktop.
public void createShortCut() {
try {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
Boolean isAppInstalled = sp.getBoolean(App.mGlobalContext, Constants.IS_APP_INSTALLED);
if (isAppInstalled) {
return;
}
editor.putBoolean(Constants.IS_APP_INSTALLED, true);
editor.apply();
Intent shortcutIntent = new Intent(getApplicationContext(), SplashActivity.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.shortcutname));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment