Determine if Android app is being used for the first time

前端 未结 16 1250
攒了一身酷
攒了一身酷 2020-11-22 12:47

I am currently developing an android app. I need to do something when the app is launched for the first time, i.e. the code only runs on the first time the program is launch

16条回答
  •  耶瑟儿~
    2020-11-22 13:30

    I made a simple class to check if your code is running for the first time/ n-times!

    Example

    Create a unique preferences

    FirstTimePreference prefFirstTime = new FirstTimePreference(getApplicationContext());
    

    Use runTheFirstTime, choose a key to check your event

    if (prefFirstTime.runTheFirstTime("myKey")) {
        Toast.makeText(this, "Test myKey & coutdown: " + prefFirstTime.getCountDown("myKey"),
                       Toast.LENGTH_LONG).show();
    }
    

    Use runTheFirstNTimes, choose a key and how many times execute

    if(prefFirstTime.runTheFirstNTimes("anotherKey" , 5)) {
        Toast.makeText(this, "ciccia Test coutdown: "+ prefFirstTime.getCountDown("anotherKey"),
                       Toast.LENGTH_LONG).show();
    }
    
    • Use getCountDown() to better handle your code

    FirstTimePreference.java

提交回复
热议问题