13

Possible Duplicate:
Is there on install event in android?

I want to execute a piece of code only once after installation complete in an Android app. This code should never be executed in the application there after.

Can anyone tell me how to do this.

Reagrds,

Shankar

Community
  • 1
  • 1
Bhabani Shankar
  • 1,217
  • 4
  • 22
  • 39

3 Answers3

33

I tried below code to make this work change it to suit your needs

SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun)
{
    // Code to run once
    SharedPreferences.Editor editor = wmbPreference.edit();
    editor.putBoolean("FIRSTRUN", false);
    //editor.commit();
    editor.apply(); 
}
ingsaurabh
  • 15,042
  • 7
  • 48
  • 77
2

You can make use of an Shared Prefrence to maintain the number the times the app has been launched. So now if the app has been launched for the first time you can execute your code, if not you can just skip it.Here is a perfect demo for it.

http://marakana.com/forums/android/examples/63.html

Andro Selva
  • 51,960
  • 51
  • 189
  • 237
0

Check out this question: Is there on install event in android?

There it is explained how to receive an event when the application is first installed/executed. You can listen to it and then execute your code.

Community
  • 1
  • 1
  • Would be great IF the app being installed could hear it. So the only option is to do what you have to do on the first start. – Dpedrinha Jun 16 '14 at 21:33