Sunday, 3 June 2018

How to detect user is inactivity for some time with multiple activity in android

I know same question is here, already asked by some one and there is answer for that,but i have issue with that answer.

My requirement is,I want to identify if user is inactivity for 5 minute then user get auto logout from app and upload the the sign in and sign out log on server.

I have tried all answer from above link,but that answer are working for Single activity,but i want to track it for multiple activity.

For that i have created abstract class

public abstract class SessionTimeOutActivity extends BaseActivity {

    public static final long DISCONNECT_TIMEOUT = 1000 * 60; // 5 min = 5 * 60 * 1000 ms

    private static Handler disconnectHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Log.d("SessionTimeOutActivity", "disconnectHandler");

            return false;
        }
    });

    private Runnable disconnectCallback = new Runnable() {
        @Override
        public void run() {
            // Perform any required operation on disconnect
            Log.d("SessionTimeOutActivity", "disconnectCallback");


            Toast.makeText(getApplicationContext(), "Session time out", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

            finish();

        }
    };

    public void resetDisconnectTimer() {
        disconnectHandler.removeCallbacks(disconnectCallback);
        disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
    }

    public void stopDisconnectTimer() {
        disconnectHandler.removeCallbacks(disconnectCallback);
    }

    @Override
    public void onUserInteraction() {
        Log.d("SessionTimeOutActivity", "onUserInteraction");
        resetDisconnectTimer();
    }

    @Override
    public void onResume() {
        super.onResume();
//        resetDisconnectTimer();
    }

    @Override
    public void onStop() {
        super.onStop();
//        stopDisconnectTimer();
    }


}

Other activities in app

    public class MenuActivtyNav extends SessionTimeOutActivity{
.....
}

MenuActivity

 public class MenuActivty extends SessionTimeOutActivity{
....
}

Issue is

1) In Lock screen Auto logout not working,it not calling disconnectCallback

2) While using app it shows toast message "Session time out"



from How to detect user is inactivity for some time with multiple activity in android

No comments:

Post a Comment