0

I will have an android application running at all times and i'm trying to code in a function where if there is no user activity for 30 seconds the app will exit.

I have looked through alot of threads on this and most of them go into far too much detail than what i'm looking for. (For example - Application idle time)

Any help would be much appreciated.

Community
  • 1
  • 1
RunningWalks
  • 119
  • 2
  • 14
  • 1
    "I will have an android application running at all times and i'm trying to code in a function where if there is no user activity for 30 seconds the app will exit" -- that sentence makes no sense to me. If we are to interpret "exit" using a conventional definition, then if you are going to "exit" the app, it will not be "running at all times". "most of them go into far too much detail than what i'm looking for" -- then please explain what you are looking for that is not covered by that answer. – CommonsWare Apr 17 '15 at 15:43
  • Simplicity if possible. It is just a query if this can be done. – RunningWalks Apr 17 '15 at 15:45
  • 2
    So, an answer with 47 upvotes that claims to offer an implementation is insufficient evidence that it can be done? Please explain what *would* be sufficient evidence. That answer is complicated, and I can think of somewhat easier solutions, but obviously at least 48 people think that the answer is fine. – CommonsWare Apr 17 '15 at 15:48

1 Answers1

1

here is what you can do take a boolean variable isInputEventOccurred and initialize it to false if any event occurs make it true.

In onCreate start a anonymous thread and after 30 seconds check if isInputEventOccurred is true or false and based on that take actions.

long timeBeforeThreadStart = System.currentTimeMillis();

boolean isInputEventOccurred = false;
Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while ((System.currentTimeMillis() - timeBeforeThreadStart) < (30 * 1000)) {
                // you add sleep here if you want instead of loop
            }
            if (!isInputEventOccurred) {
                // write code to close app
            }

        }
    });
    t.start();
Robust
  • 2,415
  • 2
  • 22
  • 37
amodkanthe
  • 4,345
  • 6
  • 36
  • 77