Counting application usage in Android

前端 未结 4 1510
南笙
南笙 2021-01-20 13:18

Can anyone help me determine how to count how many times an application has been used in Android?

4条回答
  •  情歌与酒
    2021-01-20 13:51

    Just, declare:

        private SharedPreferences prefs;
        private SharedPreferences.Editor editor;
        private int appOpened;
    

    Initialize in onCreate(...) :

    prefs = getPreferences(Context.MODE_PRIVATE);
    editor = prefs.edit();
    

    count wherever you want (any where in onCreate() or any Method of your wish):

    appOpened = prefs.getInt("counter", 0);
    appOpened++;
    
    editor.putInt("counter", appOpened);
    editor.commit();
    

    Then you use appOpened variable to do your task.

    Cheers!

提交回复
热议问题