I want to make a method (service, alarm, etc) that I can calculate after x user idle time with the app
, close the current activity
and send you to the initial activity (login)
Thank you very much
I want to make a method (service, alarm, etc) that I can calculate after x user idle time with the app
, close the current activity
and send you to the initial activity (login)
Thank you very much
You can use the CountDownTimer class, here is an example of how to use it
private long startTime=15*60*1000; // 15 MINS IDLE TIME
private final long interval = 1 * 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTimer = new MyCountDownTimer(startTime, interval);
}
@Override
public void onUserInteraction(){
super.onUserInteraction();
//Reset the timer on user interaction...
countDownTimer.cancel();
countDownTimer.start();
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
//DO WHATEVER YOU WANT HERE
// CIERRA LA APP MATANDO EL PROCESO Y VUELVE A ABRIRLO.
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
public void onTick(long millisUntilFinished) {
}
}
In the onFinish method you would have to execute the action to start another activity.
Once you have the time, in nanoseconds, you store it in time and execute the following code, the Handler is so to speak, the concurrent thread handler that will send a message to the APP (mainThread) to be executed its written code in the run () method.
int tiempo = 0; // Indícalo.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
final Intent i = new Intent(ActividadActual.this, ActividadPrincipal.class);
ActividadActual.this.startActivity(i);
ActividadActual.this.finish();
}
}, tiempo);