Close Activity if Idle

Asked

Viewed 33 times

0

I have an app and I need Activity automatically close if idle, that is, if the user does not touch the screen for 30 seconds, the Activity automatically closes. While the user is using (tapping) a Activity, it will remain open.

I’m using the method CountDownTimer, but even if the user is using the app, it closes.

Please, could someone help me?

1 answer

2


I usually create applications for interactive totem systems, which always require a timeout. I’ll try to explain how I do it:

First I create a class that extends from Application, here I’ll call her MyApplication.

Within the MyApplication, put these two attributes:

private static int seconds = 30; private static int timeOut = 30;

Where timeOut is the maximum time your screen can be idle, and seconds is the time your counter will decrease.

Once done, we will create within the MyApplication one TimerTask. Something like that:

private static class Task extends TimerTask {
    @Override
    public void run() {
        seconds--;
        if (seconds < 0) {
            stop();
           //Seu tempo acabou
        }
    }
}

Note how we decrease our seconds there.

Now all that remains is to create the functions for the use of our Task.

private static Timer timer; private static Task timerTask;

  public void start() {
    stop();

    timerTask = new Task();
    timer = new Timer();
    timer.scheduleAtFixedRate(timerTask, 1000, 1000);
}



public static void stop() {
    if (timer != null) {
        timer.cancel();
    }

    if (timerTask != null) {
        timerTask.cancel();
    }

    refresh();
}

public static void refresh() {
    seconds = timeOut;
}

Voilà, now you have the function Start to start your counter, the function Stop to stop and function refresh to assign the initial value to the meter.

For use, just call in your Activity for: MyApplication.getInstance().start()

Remember to do the refresh whenever there is an event in your application, you can use this code for this:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    MyApplication.refresh();
    return super.dispatchTouchEvent(ev);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    MyApplication.refresh();
    return super.onKeyUp(keyCode, event);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    MyApplication.refresh();
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    MyApplication.refresh();
    return super.dispatchKeyEvent(event);
}

Just don’t forget to declare your MyApplication within the tag <Application> in its manifesto.

android:name=".MyApplication"

I hope I’ve helped.

  • 1

    Thank you very much for your excellent explanation and also for the examples. I will implement in my code, because the function of my application is the same that you develop: totem. I believe it will help me. Thank you very much

  • 1

    your code worked perfectly. Thank you very much

  • Nice that it helped you! Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.