Counting how many times my Android app has been opened

后端 未结 8 1225
遇见更好的自我
遇见更好的自我 2020-12-18 07:22

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?

8条回答
  •  失恋的感觉
    2020-12-18 07:43

    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 Stack Overflow
     *
     */
    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();
    }
    

提交回复
热议问题