tags:

views:

39

answers:

4

Hello I would like to know if and how it is possible to detect if my application have run in the past on a given android device. I would like every time the phone reboots to be able to check if my application has run on the past and retrieve some private data. If not just create those data.

regards maxsap

A: 

Check if this data exists ? Or put something in the default PreferenceManager of your application

fedj
+1  A: 

You could store a simple value in the SharedPreference of the application.

insert this into your onCreate() in your Main Activity

SharedPreferences shared = getSharedPreferences("config", 0);


if (shared.getBoolean("hasRunBefore", false))
{
 // have run before.
}
else
{
SharedPreferences.Editor editor = shared.edit();
editor.putBoolean("hasRunBefore", true);
editor.commit();
 // have not run before
}
madsleejensen
A: 

You'd save everything using SharedPreferences. This will create a file readable by your app that will be created when first written to.

See the following:

Slokun
+1  A: 

As others have said, the easiest way to read and write this information is in the SharedPreferences. However, you said you want to do this every time the phone reboots. The way to go about that is to implement a BroadcastReceiver and register to receive ACTION_BOOT_COMPLETED message, and make sure to add a permission to your manifest for RECEIVE_BOOT_COMPLETED.

http://developer.android.com/guide/appendix/faq/commontasks.html#broadcastreceivers http://developer.android.com/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED http://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_BOOT_COMPLETED

Rich