Search code examples
androidtimeouttouchscreen

App timeout when not touch screen after x time


I am trying to create a lock screen when user does not touch the screen(inactive). After 30 sec I lock custom screen and I want the user to re-login but when user login the app must open before current screen. As like windows lock screen. Any one can help me. Thanks.


Solution

  • Find the solution

    Overide the below method

    long lastActivity ;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        lastActivity = new Date().getTime();
        return super.dispatchTouchEvent(ev);
    }
    

    In onResume()

    @Override
    public void onResume() {
        long now = new Date().getTime();
        if ((now - lastActivity) > Your logout  Value) {
           // do your implementation
        }
    }