Execute Android code after installation

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


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

Tags:

Android