Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
337 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...