8

in my app I got a thrad that checks every 60s data from a webservice (defined in onCreate()):

new Thread(new Runnable() {
    @Override
    public void run() {     
        while (true) {
            try {               
                Thread.sleep(PERIOD);
                if(condition) do_something();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}).start();

Additionally i got a handler executing a method after a period of user inactivity:

private Handler disconnectHandler = new Handler();

private Runnable disconnectCallback = new Runnable() {
    @Override
    public void run() {
        do_something_else();
    }
};

public void resetDisconnectTimer(){
    disconnectHandler.removeCallbacks(disconnectCallback);
    disconnectHandler.postDelayed(disconnectCallback, TIMEOUT);
}

@Override
public void onUserInteraction() {
    super.onUserInteraction();
    resetDisconnectTimer();
}

Now I got the problem that onUserInteraction() is called after PERIOD too and not only after TIMEOUT. Any ideas to get both working?

Thanks in advance.

Second2None
  • 462
  • 2
  • 8
  • 22

2 Answers2

8

may be you want read the source below:

Instrumentation.java

public void callActivityOnUserLeaving(Activity activity) {
   activity.performUserLeaving();}

Activity.java

final void performUserLeaving() {
    onUserInteraction();
    onUserLeaveHint();}

public boolean dispatchKeyEvent(KeyEvent event) {
    onUserInteraction();
    ...
}
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
    onUserInteraction();
    .....
}
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        onUserInteraction();
    }
  ....
}
public boolean dispatchTrackballEvent(MotionEvent ev) {
    onUserInteraction();
   ....
}
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
    onUserInteraction();
   ....
}

onUserInteraction() be called as below:

  1. as the doc ,onUserInteraction Called whenever a key, touch, or trackball event is dispatched to the activity. "interacted"is significant. So all "XXEvent" will call this method.
  2. doc : "All calls to your activity's onUserLeaveHint() callback will be accompanied by calls to onUserInteraction()." onUserLeaveHint() : Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. that is, when u is going to leave activity, it be called. such as , start another activity in your act.
Evan
  • 266
  • 4
  • 5
2

The Android documentation makes this very clear. It states :

public void onUserInteraction ()

Called whenever a key, touch, or trackball event is dispatched to the activity. Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running. This callback and onUserLeaveHint() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

All calls to your activity's onUserLeaveHint() callback will be accompanied by calls to onUserInteraction(). This ensures that your activity will be told of relevant user activity such as pulling down the notification pane and touching an item there.

Note that this callback will be invoked for the touch down action that begins a touch gesture, but may not be invoked for the touch-moved and touch-up actions that follow.

CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
  • 1
    I read the documentation, but onUserInteraction() is called when I do not touch the device at all. So there is no key, touch or trackball event... Commenting out the Thread workes fine for the handler to work correctly. – Second2None Jul 18 '14 at 12:21
  • @Second2None As far as i know it's called only when the user interacts with the device else not. Maybe there is some logical error in your code. Kindly have a look at your code again. – CodeWarrior Jul 18 '14 at 12:29
  • Check detailed example here - https://stackoverflow.com/questions/4208730/how-to-detect-user-inactivity-in-android – Rohit Patil Nov 30 '21 at 11:30