Close and go to an activity after the user's idle time [closed]

3

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

    
asked by Gerard_jcr 27.04.2016 в 18:19
source

2 answers

5

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.

    
answered by 27.04.2016 / 18:35
source
0

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);
    
answered by 27.04.2016 в 18:26