在我的应用程序中,我得到了一个线程,它每 60 秒检查一次来自网络服务的数据(在 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();
另外,我有一个处理程序在用户不活动一段时间后执行一个方法:
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();
}
现在我遇到的问题是 onUserInteraction() 也在 PERIOD 之后被调用,而不仅仅是在 TIMEOUT 之后。 有什么想法可以让两者都起作用吗?
提前致谢。
最佳答案
也许你想阅读下面的来源:
仪器.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() 调用如下:
- 作为文档,onUserInteraction 在按键、触摸或轨迹球事件被调度到 Activity 时调用。 “互动”很重要。所以所有的“XXEvent”都会调用这个方法。
- doc:“对您 Activity 的 onUserLeaveHint() 回调的所有调用都将伴随对 onUserInteraction() 的调用。” onUserLeaveHint() :在 Activity 即将结束时作为 Activity 生命周期的一部分调用 作为用户选择的结果进入后台。也就是说,当你要离开 Activity 时,它会被调用。例如,在您的行为中开始另一项 Activity 。
关于android - onUserInteraction() 到底什么时候被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24824528/