Gatiko06
Gatiko06

Reputation: 299

I want create shortcut

I create shortcut when open the app but the problem is that create a shortcut always that open the app if I open the app 20 time then creates 20 shortcuts

I need that only create one shortcut the first open not 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);
}

Upvotes: 1

Views: 2335

Answers (3)

Ksharp
Ksharp

Reputation: 102

Yes.You can use SharePreferences to check if the App is firstly starting. More information check this:

http://pulse7.net/android/

Upvotes: 0

Michael
Michael

Reputation: 3739

You can store a flag in preference manager to mark whether you've created the shortcut. This can be achieved by

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);
} 

Upvotes: 0

Amokrane Chentir
Amokrane Chentir

Reputation: 30385

On pre-JB android versions, you can try this:

addIntent.putExtra("duplicate", false);

Otherwise, you can just uninstall 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 info here.

Upvotes: 9

Related Questions