Counting how many times my Android app has been opened

后端 未结 8 1291
遇见更好的自我
遇见更好的自我 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:52

      Using SharedPreference or the Database.
    
    during OnCreate add 1 to the numberofTimes counter and commit.
    
    OnCreate (Bundle bundle){
      mPref = getPreferences();
      int c = mPref.getInt("numRun",0);
      c++;
      mPref.edit().putInt("numRun",c).commit();
      //do other stuff...
    }
    
    OnCreate is called regardless of you start the app or you resume the app, but isFinishing() returns true if and only iff the user (or you) called finish() on the app (and it was not being destroyed by the manager)
    
    This way you only increment when you are doing fresh start.
    
    the onFinishing() Method inside of a OnPause method to check to see if the activity is being finish() or just being paused.
    
    @Override
    protected void OnPause(){
      if(!onFinishing()){
        c = mPref.getInt("numRun",0);
        c--;
        mPref.edit().putInt("numRun",c).commit();
      }
      //Other pause stuff.
    }
    

提交回复
热议问题