Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Android code after installation [duplicate]

Tags:

android

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

like image 405
Bhabani Shankar Avatar asked Aug 22 '11 07:08

Bhabani Shankar


2 Answers

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(); 
}
like image 129
ingsaurabh Avatar answered Oct 26 '22 20:10

ingsaurabh


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

like image 33
Andro Selva Avatar answered Oct 26 '22 21:10

Andro Selva