究竟何时调用onUserInteraction()? [英] When exactly is onUserInteraction() called?

查看:460
本文介绍了究竟何时调用onUserInteraction()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我得到了一个风潮,它可以检查来自Web服务的每60s数据(在onCreate()中定义):

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();
}

现在我遇到的问题是,在PERIOD之后也不仅在TIMEOUT之后也调用onUserInteraction(). 有什么想法可以使两者都起作用吗?

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

谢谢.

推荐答案

也许您想阅读下面的源代码:

may be you want read the source below:

Instrumentation.java

Instrumentation.java

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


Activity.java


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()如下调用:

onUserInteraction() be called as below:

  1. 作为doc,onUserInteraction每当将按键,触摸或轨迹球事件调度到活动时调用. 相互作用"是重要的.因此所有"XXEvent"都将调用此方法.
  2. doc:对活动的onUserLeaveHint()回调的所有调用都将伴随对onUserInteraction()的调用." onUserLeaveHint():在活动即将进行时作为活动生命周期的一部分被调用 用户选择的结果进入后台.也就是说,当您打算离开活动时,它会被调用.例如,在您的行为中开始另一项活动.
  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.

这篇关于究竟何时调用onUserInteraction()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆