Reputation: 640
I want to write a mobile application(android) using Phonegap and I want to detect first use of my application in order to perform some activation process. How can I detect is this a first use of my application or not? Should I use any database?or something like cookies?
Upvotes: 3
Views: 541
Reputation: 1810
You write this function to anywhere :
public static String LoadPreferencesString(Context con, String key) {
// ContextWrapper wrapper=new ContextWrapper(con);
SharedPreferences sharedPreferences = con.getSharedPreferences(
"yourxmlfilename", Context.MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString(key, "");
return strSavedMem1;
}
public static void SavePreferencesString(Context con, String key, String value) {
SharedPreferences sharedPreferences = con.getSharedPreferences(
"yourxmlfilename", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
You can write a value with key to xml file which youidentified name at first run time. The other you check the value is null. If value is null(or space( )) the app is running first time.
Upvotes: 0
Reputation: 103
Well, I never tried PhoneGap, but the usual approach (one that I did with Android and iPhone, both natively ) is to write some bytes to a file. Then, every time you open it, you try read the file. In case you cant open it, it is the first time.
In case it is not the first time and still it fails, you will "fail graciously" by starting over everything and recover as if the failure never happened.
I would also recommend you that those bytes to be the "first run" UNIX time. That way, if you never need to know the time, you can ignore it. But in case you ever need it, its already there. I would even prepend a small header followed by a byte count, so you can easily extend this format in the future.
That's my 2 cents ;-)
Upvotes: 2