Skip to content Skip to sidebar Skip to footer

Know When To Show A Passcode Lock

I am developing an application that needs to display a passcode screen whenever a user leaves the app and comes back (be it through a screen lock, or going back to the home screen

Solution 1:

In onStop() of each activity, update a static data member with the time you left the activity. In onStart() of each activity, check that time, and if it exceeds some timeout threshold, display your authentication activity. Allow the user to set the timeout value, so that if they don't want to be bothered every few seconds, they can control that.

Solution 2:

I liked the time based approach, I've been struggling for a while getting this to work in a nice way. The time based approach works well. I made a class for easier usage.

publicclassPinCodeCheck {

    privatestaticlong INIT_TIME           = 0;
    privatestatic PinCodeCheck ref         = null;
    privatestatic SharedPreferences values = null;

    privatePinCodeCheck(Context context){
        values = context.getSharedPreferences("com.example.xxx", 0); //use your preferences file name key!
    }//end constructorpublicstatic synchronized PinCodeCheck getInstance(Context context) {
        if (ref == null){
            ref = new PinCodeCheck(context);
        } 
        returnref;
    }//end method publicvoidinit(){
        PinCodeCheck.INIT_TIME = System.currentTimeMillis();    
    }//end method   publicvoidforceLock(){
        PinCodeCheck.INIT_TIME = 0;     
    }//end method   public boolean isLocked(){
        long currentTime    = System.currentTimeMillis();
        long threshold      = values.getLong(Keys.PASSWORD_PROTECT_TIMEOUT, 30000); // check here, might change in between callsif (currentTime - PinCodeCheck.INIT_TIME > threshold){
            returntrue;
        }
        returnfalse;       
    }//end method   
}//end class

USAGE

privatestaticPinCodeCheck check   = PinCodeCheck.getInstance(context);

@OverridepublicvoidonResume() {
    super.onResume();  

    if (check.isLocked()) { 
        showDialog();               
    }
}//end method @OverridepublicvoidonPause() {          
    super.onPause();

    check.init();
}//end method 

Post a Comment for "Know When To Show A Passcode Lock"