I am developing an android app and I want to know how many times it has been opened. Is there a way to do this?
Android OS users can find phone usage stats under the Digital Wellbeing section. Through it, users can access various daily and weekly phone usage stats as well as parental controls. Users can also see how much time is spent on apps and set timers and daily limits for individual websites.
The problem with using onCreate
in an Activity
is that this will increment the counter even on orientation changes. Using onCreate
in an Application
also has a downside in that your counter will only be incremented when after the VM has closed - so even if the app exits and reopens this will not necessarily increment.
The truth is there is no fool-proof method for handling this sort of count, however I have come up with a very good way of doing this, that is about as close to 100% accurate as possible. It requires work in both an Application
class and your main Activity
class, and relies on timestamps to differentiate between orientation changes and actual app launches. To start, add the following Application
class:
/**
* Application class used for correctly counting the number of times an app has been opened.
* @author Phil Brown
* @see <a href="http://stackoverflow.com/a/22228198/763080">Stack Overflow</a>
*
*/
public class CounterApplication extends Application
{
private long lastConfigChange;
/** @param buffer the number of milliseconds required after an orientation change to still be considered the same event*/
public boolean wasLastConfigChangeRecent(int buffer)
{
return (new Date().getTime() - lastConfigChange <= buffer);
}
@Override
public void onCreate()
{
lastConfigChange = new Date().getTime();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
lastConfigChange = new Date().getTime();
}
}
You should add this Application to your AndroidManifest.xml
by specifying the name application attribute:
android:name="path.to.CounterApplication"
Now, in your main Activity
, add the following in onCreate
:
//note that you can use getPreferences(MODE_PRIVATE), but this is easier to use from Fragments.
SharedPreferences prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
int appOpenedCount = prefs.getInt("app_opened_count", 1);
if (!((CounterApplication) getApplication()).wasLastConfigChangeRecent(10000))//within 10 seconds - a huge buffer
{
appOpenedCount += 1;
prefs.edit().putInt("app_opened_count", appOpenedCount).commit();
}
//now say you want to do something every 10th time they open the app:
boolean shouldDoThing = (appOpenedCount % 10 == 0);
if (shouldDoThing)
{
doThing();
appOpenedCount += 1;
//this ensures that the thing does not happen again on an orientation change.
prefs.edit().putInt("app_opened_count", appOpenedCount).commit();
}
Just, declare:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private int totalCount;
Initialize in onCreate(...) :
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
Print or count wherever you want (any where in onCreate or any specific click as you specified)
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit();
Now print totalcount where you want to count eg.:
System.out.println("Total Application counter Reach to :"+totalCount);
In your Application or Activity's onCreate()
method, increment a counter stored in persistent storage such as SharedPreferences
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With