android How to detect if the application have run in the past - android

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.

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
}

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

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

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:
Tutorial
Documentation

Related

How to save number of times a user has tapped on a view

I am designing a simple application that will count how many times a user has tapped on a imageView. My question is what would be the best way of saving and reading this file. Any suggestions? I am thinking something like using Parse.com's local database. I have tried it, but I could not get it working the way I wanted. I am still a beginner, so please not so fancy suggestions.
Try to save data in SharedPreference. SharedPreference works like database for application on device that will be stored until any one has unistall app from device.
To create sharedPrefernce-
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To store data -
prefs.edit().putInt("key", int_value).apply();
To retrieve data-
// use a default value
int l = prefs.getLong("key", default_value);
Simplest options is always thebest option, go with shared preferences
Here is simple tutorial from google http://developer.android.com/training/basics/data-storage/shared-preferences.html
It will store your data in application local file. Take a note of that there are different shared preferences in example getPreferences() will return file specific for activity you used this method. While getSharedPreferences() will return application global file.

Android SharedPreference do not working

I am writing a code to the app execute a function only once. The problem that the getSharedPreference always return false (see first line) even if I close and open the application.
boolean firstboot = context.getSharedPreferences("BOOT_PREF",context.MODE_PRIVATE).getBoolean("firstboot", true);
if(firstboot)
{
context.getSharedPreferences("BOOT_PREF",context.MODE_PRIVATE).edit().putBoolean("firstboot", false).commit();
Log.d(TAG, "first" );
}
else
Log.d(TAG, "not first time" );
What I am doing wrong?
Best regards
If you open and close the application the preferences won't change, that's the whole point of shared preferences, think of it as a simple database that stores unique information.
If you want to reset the shared preferences you can either: uninstall and reinstall the application, force close the application and clear all data or add the code to clear it:
context.getSharedPreferences("BOOT_PREF", context.MODE_PRIVATE).edit().clear().commit();

How do I count the number of times an Android application is used? [duplicate]

Can anyone help me determine how to count how many times an application has been used in Android?
Write to a SharedPreferance onCreate. It won't be a very accurate count since onCreate is called at times other than only application start-up, but it'll be a reasonably good figure.
If you offer more details as to why you're doing this, you may get a more detailed answer.
Just, declare:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private int appOpened;
Initialize in onCreate(...) :
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
count wherever you want (any where in onCreate() or any Method of your wish):
appOpened = prefs.getInt("counter", 0);
appOpened++;
editor.putInt("counter", appOpened);
editor.commit();
Then you use appOpened variable to do your task.
Cheers!
You could have a file which stores one number, not on the SD card but locally for your app. Then open this and increment the number in your onCreate method. You can also keep track of when they do other things but don't actually close it with onPause and onResume... There might be another way to save data without explicitly creating a file...
by using Flurry we can do this.
The Flurry Android Analytics Agent allows you to track the usage and behavior of your Android application on users' phones for viewing in the Flurry Analytics system

Counting application usage in Android

Can anyone help me determine how to count how many times an application has been used in Android?
Write to a SharedPreferance onCreate. It won't be a very accurate count since onCreate is called at times other than only application start-up, but it'll be a reasonably good figure.
If you offer more details as to why you're doing this, you may get a more detailed answer.
Just, declare:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private int appOpened;
Initialize in onCreate(...) :
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
count wherever you want (any where in onCreate() or any Method of your wish):
appOpened = prefs.getInt("counter", 0);
appOpened++;
editor.putInt("counter", appOpened);
editor.commit();
Then you use appOpened variable to do your task.
Cheers!
You could have a file which stores one number, not on the SD card but locally for your app. Then open this and increment the number in your onCreate method. You can also keep track of when they do other things but don't actually close it with onPause and onResume... There might be another way to save data without explicitly creating a file...
by using Flurry we can do this.
The Flurry Android Analytics Agent allows you to track the usage and behavior of your Android application on users' phones for viewing in the Flurry Analytics system

Parsing XML one time only on 1st startup

I'm currently using a Parser and it parses every time the onCreate gets called.
My parser uses an XML file that's located in my assetfolder.
Is there any chance that I can make it parse only once, instead of every time I rotate the screen or restart the application?
Thank you :)
There are 2 answers to this question. The easy one first:
If you only want this to happen once, create a static boolean in your main activity and set it to true when the XML is parsed in. Then use:
if(!isParsed)
XML.parse();
The more complete answer to this question is to make sure you are handling configuration changes correctly.
I suggest you read this article on how to implement configuration change handling. You should be able to solve this problem "the Android way" by implementing this.
Hope this helps!
Persist a flag using the Preferences system the first time you complete a parse, and check this each time before parsing again. Do all this in your OnCreate() method.
SharedPreferences settings = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
if (!settings.getBoolean("parsed", false)) {
parseTheXML();
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("parsed", true);
editor.commit();
}
There are other ways but this has the advantage of working across app reloads/phone power cycles - assuming that's what you want.
You could add a preference. Something like -
void setIsAppOpened() {
editor.putBoolean(APP_OPENED, true);
editor.commit();
}
Set it in your onCreate(). Then, also add the following code to check if the preference exists.
if (!prefs.contains(APP_OPENED))
{
//parse the XML
setIsAppOpened();
}
You can parse your XML in a "Application" object and have it accessible from there. Check out the documentation at http://developer.android.com/reference/android/app/Application.html

Categories

Resources