与 UI 线程 Android 通信:用户不活动
Communicating with the UI Thread Android: User Inactivity
我有一个 Android 应用程序,它通过 Handler
验证用户不活动。但我想知道我的 Handler
实施是否是我的最佳解决方案。
代码:
public abstract class UserInteractionControlActivity extends Activity {
private static final int SESSION_TIME_OUT = 300000;
private static final int SESSION_DELAY_TIME = 60000;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private long mLastUserInterationTime;
protected void onResume() {
super.onResume();
mHandler.postDelayed(new UserInteractionControl(), SESSION_TIME_OUT);
Log.i("LOG_KEY", "HANDLER FIRST POST!");
}
public void onUserInteraction() {
super.onUserInteraction();
mLastUserInterationTime = System.currentTimeMillis();
}
private final class UserInteractionControl implements Runnable {
public void run() {
long currentTime = System.currentTimeMillis();
long inactiveTime = currentTime - mLastUserInterationTime;
if (inactiveTime > SESSION_TIME_OUT) {
Log.i("LOG_KEY", "TIME OUT!");
} else {
mHandler.postDelayed(new UserInteractionControl(), SESSION_DELAY_TIME);
Log.i("LOG_KEY", "HANDLER POST AGAIN!");
}
}
}
}
我的主要问题是:
1) 使用 new Handler(Looper.getMainLooper())
和 getWindow().getDecorView().getHandler()
实例化 Handler
有什么区别?
2) 在这种情况下使用 System.currentTimeMillis()
是安全的吗?
1) 正如@corsair992 所说,它们应该是等效的,但是我认为拥有自己的 Handler
实例更好,因为您可以明确控制它。如果您稍后需要删除任何待处理的 Runnable
实例,您只需执行 removeCallbacksAndRunnables(null);
即可删除您的 Handler
发布的任何消息,而不会影响装饰视图的 Handler
。
2) 不要使用System.currentTimeMillis()
。 SystemClock.elapsedRealtime()
是经过时间的更好指示器,因为它不会受到用户更改 Date/Time 的影响(而 currentTimeMillis()
会 更改).
我有一个 Android 应用程序,它通过 Handler
验证用户不活动。但我想知道我的 Handler
实施是否是我的最佳解决方案。
代码:
public abstract class UserInteractionControlActivity extends Activity {
private static final int SESSION_TIME_OUT = 300000;
private static final int SESSION_DELAY_TIME = 60000;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private long mLastUserInterationTime;
protected void onResume() {
super.onResume();
mHandler.postDelayed(new UserInteractionControl(), SESSION_TIME_OUT);
Log.i("LOG_KEY", "HANDLER FIRST POST!");
}
public void onUserInteraction() {
super.onUserInteraction();
mLastUserInterationTime = System.currentTimeMillis();
}
private final class UserInteractionControl implements Runnable {
public void run() {
long currentTime = System.currentTimeMillis();
long inactiveTime = currentTime - mLastUserInterationTime;
if (inactiveTime > SESSION_TIME_OUT) {
Log.i("LOG_KEY", "TIME OUT!");
} else {
mHandler.postDelayed(new UserInteractionControl(), SESSION_DELAY_TIME);
Log.i("LOG_KEY", "HANDLER POST AGAIN!");
}
}
}
}
我的主要问题是:
1) 使用 new Handler(Looper.getMainLooper())
和 getWindow().getDecorView().getHandler()
实例化 Handler
有什么区别?
2) 在这种情况下使用 System.currentTimeMillis()
是安全的吗?
1) 正如@corsair992 所说,它们应该是等效的,但是我认为拥有自己的 Handler
实例更好,因为您可以明确控制它。如果您稍后需要删除任何待处理的 Runnable
实例,您只需执行 removeCallbacksAndRunnables(null);
即可删除您的 Handler
发布的任何消息,而不会影响装饰视图的 Handler
。
2) 不要使用System.currentTimeMillis()
。 SystemClock.elapsedRealtime()
是经过时间的更好指示器,因为它不会受到用户更改 Date/Time 的影响(而 currentTimeMillis()
会 更改).