Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting how many times my Android app has been opened

Tags:

java

android

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?

like image 593
Rameez Hussain Avatar asked Aug 23 '12 13:08

Rameez Hussain


People also ask

Can you see how much time you spend on an app Android?

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.


3 Answers

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();
}
like image 120
Phil Avatar answered Nov 04 '22 16:11

Phil


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);
like image 27
Nirav Mehta Avatar answered Nov 04 '22 15:11

Nirav Mehta


In your Application or Activity's onCreate() method, increment a counter stored in persistent storage such as SharedPreferences.

like image 30
Graham Borland Avatar answered Nov 04 '22 16:11

Graham Borland