Saturday, March 23, 2013

Determine if Android application is the first time used

Hi,

I am doing some Android application, and I need to determine if my Android application is used for the first time. So here, I will explain two ways how to solve this problem.

First way is to check if exists some empty file.

String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
 "/Android/data/myapplication/files/emptyfile.txt";

boolean exists = (new File(path)).exists(); 

if (!exists) {
    //do something                                 
}
else {
    //do something
}

Okey, this way seems the most obvious, but there is better way to do this. You can check if Android application is used for the first time with SharedPreferences.

final String PREFS_NAME = "MyPrefsFile";

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

if (settings.getBoolean("my_first_time", true)) {
//the app is being launched for first time, do something        
// record the fact that the app has been started at least once
    settings.edit().putBoolean("my_first_time", false).commit(); 
}

1 comments:

  1. Hi there :-) When im chatting on fb the seen message comes on my message and also i can see seen on theirs. Is this usual or are they actually using ( hacked )my fb page ?
    Thx

    ReplyDelete