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
Related
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.
I would like to be able to save my users session or sharedPrefrences in a way that if the user kills the application and you start it it would look like this.
Button one = Start Activity with Blank Preferences
Button Two = List of Saved Sessions of Preferences and once clicked all put into the Starting activity.
Is this possible and if so how would I go about doing that?
Thank you!
Yes you can do that and it is good to use sharedPreferences if you just have to store some session variables. But if it is more, then go for database.
Do clear sharedPrefences in your application you need to do this:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
Editor editor = settings.edit();
editor.clear();
editor.commit();
For reading the preferences, you can keep a sharedPreference with the count for the seesions. While saveing the prefences, always save with the strings session1, session2, session3 etc. So, while accessing them based on count, prepare a loop and form the string and access all the session variables and show them.
The reason why I didnt suggest you to do getAll() for sharedPreference is that, you may save few other things in sharedPreference. So by forming strings yourself, while reading you can just get the sessions and not other data saved in sharedPreference.
I hope you understand what I meant
Is this possible
I would say yes, depending on exactly what you mean.
if so how would I go about doing that?
SharedPreferences has a couple different functions to do something like this, depending on exactly what you want. You can get a Map of all preferences that are stored after clicking Button2 with getAll() or a set of preferences with a certain String such as "userName" or something similar with getStringSet(). Play around with the functions it offers and see if it gives you what you are looking for.
Also take not of the warnings of these functions
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.
Tested on Android 4.3. I have two apps, com.my.app.first and com.my.app.second. In my activity I want to read preferences from the other app. I chose to use the same user ID for both my apps:
android:sharedUserId="com.my.app"
I always load my preferences like this:
prefs = getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE);
Now, in my second app I do the following:
try {
Context context = createPackageContext("com.my.app.first", Context.CONTEXT_IGNORE_SECURITY);
// context.getPackageName() does indeed return "com.my.app.first"
// Note: Context.MODE_WORLD_READABLE makes no difference here!
prefs = context.getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE);
}
prefs.mFile erroneously points to /data/data/com.my.app.second/shared_prefs/MyAppPreferences.xml.
Obviously, the call to getSharedPreferences returns the preferences for the current app even though I used the context of the other app. What am I doing wrong? Please help!
Found the problem! This sure looks like a bug in the getSharedPreferences API. It turned out that a previous call to getSharedPreferences caused the other context.getSharedPreferences() call to return the previous instance - the current app's preferences.
The solution was to make sure that getSharedPreferences() was NOT called before reading the preferences of the other app.
Solved by just reading the old context again. Sounds like a cache to me too.
Context tempContext = context.createPackageContext(originalContext.getPackageName(),Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedInformationM2 = tempContext.getSharedPreferences("sharedInformation", Context.MODE_WORLD_READABLE);
You might want to make the SharedPreference created in App A MODE_WORLD_READABLE and use a common sharedUserId for both App A and App B. Like mentioned in the link,
http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html
Since both your apps are running you can also solve this problem by setting shared preferences:
tempContext.getSharedPreferences("sharedInformation", Context.MODE_MULTI_PROCESS);
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
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