It occurs to me that this idea is the simplest, detecting when the screen has been blocked:
We add a variable to validate if the screen was blocked and another to validate when the application was changed:
private boolean seBloqueoPantalla;
private boolean cambioAplicacion;
within onPause()
we change the value of the variable seBloqueoPantalla
to "true", if we detect screen lock:
@Override
protected void onPause() {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (!isScreenOn) {
Log.i(TAG, "La pantalla ha sido bloqueada");
seBloqueoPantalla = true;
}
super.onPause();
}
and using the onStop()
method we change the value of the variable cambioAplicacion
to "true", if we detect that we change the application:
@Override
protected void onStop() {
cambioAplicacion = true;
super.onStop();
}
Using the onResume()
method, you can perform an "login" activity, depending on the state of the variable seBloqueoPantalla
and cambioAplicacion
:
@Override
protected void onResume() {
if(seBloqueoPantalla && cambioAplicacion) {
//Redirecciona a la actividad Login!.
Intent myIntent = new Intent(this, LoginActivity.class);
startActivity(myIntent);
//reiniciamos valores.
seBloqueoPantalla = false;
cambioAplicacion = false;
}
super.onResume();
}