zxing二维码扫描界面销毁问题
来源:互联网 发布:淘宝股份有日本人多少 编辑:程序博客网 时间:2024/06/09 20:13
InactivityTimer控制扫描界面销毁时间控制
- public final class InactivityTimer {
-
- private static final class DaemonThreadFactory implements ThreadFactory {
- @Override
- public Thread newThread(Runnable runnable) {
- final Thread thread = new Thread(runnable);
- thread.setDaemon(true);
- return thread;
- }
- }
-
- private static final int INACTIVITY_DELAY_SECONDS = 5 * 60;
- private final Activity activity;
- private ScheduledFuture<?> inactivityFuture = null;
-
- private final ScheduledExecutorService inactivityTimer =
- Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory());
-
- public InactivityTimer(Activity activity) {
- this.activity = activity;
- onActivity();
- }
-
- private void cancel() {
- if (inactivityFuture != null) {
- inactivityFuture.cancel(true);
- inactivityFuture = null;
- }
- }
-
- public void onActivity() {
- cancel();
- inactivityFuture = inactivityTimer.schedule(new FinishListener(activity),
- INACTIVITY_DELAY_SECONDS,
- TimeUnit.SECONDS);
- }
-
- public void shutdown() {
- cancel();
- inactivityTimer.shutdown();
- }
-
- }
FinishListener 类:-
-
-
-
- public final class FinishListener
- implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener, Runnable {
-
- private final Activity activityToFinish;
-
- public FinishListener(Activity activityToFinish) {
- this.activityToFinish = activityToFinish;
- }
-
- @Override
- public void onCancel(DialogInterface dialogInterface) {
- run();
- }
-
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- run();
- }
-
- @Override
- public void run() {
- activityToFinish.finish();
- }
-
- }
0 0