zxing二维码扫描界面销毁问题

来源:互联网 发布:淘宝股份有日本人多少 编辑:程序博客网 时间:2024/06/09 20:13

InactivityTimer控制扫描界面销毁时间控制

  1. public final class InactivityTimer {  
  2.   
  3.     private static final class DaemonThreadFactory implements ThreadFactory {  
  4.         @Override  
  5.         public Thread newThread(Runnable runnable) {  
  6.             final Thread thread = new Thread(runnable);  
  7.             thread.setDaemon(true);  
  8.             return thread;  
  9.         }  
  10.     }  
  11.   
  12.     private static final int INACTIVITY_DELAY_SECONDS = 5 * 60;  
  13.     private final Activity activity;  
  14.     private ScheduledFuture<?> inactivityFuture = null;  
  15.   
  16.     private final ScheduledExecutorService inactivityTimer =  
  17.             Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory());  
  18.   
  19.     public InactivityTimer(Activity activity) {  
  20.         this.activity = activity;  
  21.         onActivity();  
  22.     }  
  23.   
  24.     private void cancel() {  
  25.         if (inactivityFuture != null) {  
  26.             inactivityFuture.cancel(true);  
  27.             inactivityFuture = null;  
  28.         }  
  29.     }  
  30.   
  31.     public void onActivity() {  
  32.         cancel();  
  33.         inactivityFuture = inactivityTimer.schedule(new FinishListener(activity),  
  34.                 INACTIVITY_DELAY_SECONDS,  
  35.                 TimeUnit.SECONDS);  
  36.     }  
  37.   
  38.     public void shutdown() {  
  39.         cancel();  
  40.         inactivityTimer.shutdown();  
  41.     }  
  42.   
  43. }  

FinishListener 类:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Simple listener used to exit the app in a few cases. 
  3.  *  
  4.  */  
  5. public final class FinishListener  
  6.         implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener, Runnable {  
  7.   
  8.     private final Activity activityToFinish;  
  9.   
  10.     public FinishListener(Activity activityToFinish) {  
  11.         this.activityToFinish = activityToFinish;  
  12.     }  
  13.   
  14.     @Override  
  15.     public void onCancel(DialogInterface dialogInterface) {  
  16.         run();  
  17.     }  
  18.   
  19.     @Override  
  20.     public void onClick(DialogInterface dialogInterface, int i) {  
  21.         run();  
  22.     }  
  23.   
  24.     @Override  
  25.     public void run() {  
  26.         activityToFinish.finish();  
  27.     }  
  28.   
  29. }  

0 0
原创粉丝点击