0

I want to install the shortcut on the home screen when I install the application on any phone, I have tried a code for it and also it works perfect but it creates a new shortcut every time I start the application(If I start application 3 times then it creates the 3 shortcut on homescreen) please help me for this,

here is my code,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addshortcut();

    setContentView(R.layout.activity_splash);

    ivsplash=(ImageView)findViewById(R.id.ivsplash);

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent i = new Intent(Splash.this, Home.class);
            startActivity(i);
            finish();
        }
    },SPLASH_TIME_OUT);


}



private void addshortcut() {
    // TODO Auto-generated method stub

    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, "anaxus");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.applogo1));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

thank you.

akky777
  • 423
  • 2
  • 6
  • 23

3 Answers3

1

Because onCreate() is called everytime your application is started. You know that the Play store automatically places a shortcut on your homescreen after installation?

Anyway, your question has already been answered here: Android create shortcuts on the home screen

Edit: The code taken from above post;

XML (manifest):

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

Java:

// create shortcut if requested
ShortcutIconResource icon =
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

Intent intent = new Intent();

Intent launchIntent = new Intent(this,ActivityToLaunch.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

setResult(RESULT_OK, intent);
Community
  • 1
  • 1
Alex van den Hoogen
  • 744
  • 1
  • 5
  • 22
0

Set a bool in shared preference for application launch for first time in life of app and once app launches set it to false and based on the condition call your addShortcut() method. It will prevent calling the function every time when app is launched.

public static boolean isAppRunningFirstTime(Context context, String filename)
{
    SharedPreferences sharedPreferences = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
    boolean isAppRunningForFirstTime = sharedPreferences.getBoolean(context.getString(R.string.app_name), true);
    return isAppRunningForFirstTime;
}

public static void setAppRunningForFirstTime(Context context, String filename)
{
    SharedPreferences sharedPreferences = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(context.getString(R.string.app_name), false);
    editor.commit();
}

Once your shorcut adding code is done, set the bool to false.

Bette Devine
  • 1,196
  • 1
  • 9
  • 23
0

Your question isn't explicit, but I'm assuming that you're after a way to make sure duplicate shortcuts aren't created. Unfortunately, there's no direct way to learn which icons are on already on the homescreen. You wouldn't want to do it that way anyway. If a user manually deleted the shortcut after installation, they would be annoyed if you kept recreating it.

You'll want your app itself to keep track of whether it's already created the shortcut. The easiest way is with a SharedPreference item. Here's an example from the Android Developers Storage Options page.

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();
    }
}
scottt
  • 8,301
  • 1
  • 31
  • 41
  • Typed too slow; the answer by Bette Devine actually contains code tailored to your situation. But I'll leave this answer since the narrative and link might be helpful to someone. – scottt Mar 14 '14 at 11:12