I want to create a shortcut

I create a shortcut when the application opens, but the problem is that always create a shortcut that opens the application if I open the application 20 times and then create 20 shortcuts

I need to create only one shortcut, the first to open no more

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ShortcutIcon(); } private void ShortcutIcon(){ Intent shortcutIntent = new Intent(getApplicationContext(), Main.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, "Whatsapp Imagenes"); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icono)); addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); } 
+1
android
Jan 01 '14 at 1:19
source share
3 answers

In Android versions prior to JB, you can try the following:

 addIntent.putExtra("duplicate", false); 

Otherwise, you can simply remove and reinstall the shortcut:

 intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(intent); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(intent); 

More details here .

+9
Jan 01 '14 at 1:22
source share

You can save the flag in the preferences manager to indicate whether you have created a shortcut. This can be achieved using

 SharedPreferences prefs = mContext.getSharedPreferences("shortcut_created", 0); SharedPreferences.Editor editor = prefs.edit(); if (prefs.getBoolean("shortcut_created", false)) { // if shortcut has not been created, create your shortcut here ... // once shortcut is created, mark the preference so the next time it does not // create the shortcut again editor.putBoolean("shortcut_created", true); } 
0
Jan 01 '14 at 1:26
source share

Yes. You can use SharePreferences to check if the application starts. Additional Information:

http://pulse7.net/android/

0
Jan 01 '14 at 5:31
source share



All Articles