Android如何在应用程序一段时间无操作后做一些事情(退出登录)

项目中遇到一个需求,程序在一段时间没有操作的情况下退出登录,以此作为随笔记忆。

  • 原理:在BaseActivity中重写dispatchTouchEvent方法,在MotionEvent.ACTION_UP事件中利用Handler发送一个延时消息,在MotionEvent.ACTION_DOWN事件中清空Handler消息队列,然后所有的Activity继承BaseActivity。

  • 下面贴出BaseActivity代码:

      /**
       * Created by 一位不愿透露自己姓氏的先生 on 2017/8/23.
       */
    
          public class BaseActivity extends AppCompatActivity {
          
              @Override
              protected void onCreate(@Nullable Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  startAD();
              }
          
              private Handler handler = new Handler();
              private long time = 1000 * 5;
          
              @Override
              public boolean dispatchTouchEvent(MotionEvent event) {
                  switch (event.getAction()) {
                      case MotionEvent.ACTION_DOWN:
                          handler.removeCallbacks(runnable);
                          break;
                      case MotionEvent.ACTION_UP:
                          startAD();
                          break;
                  }
                  return super.dispatchTouchEvent(event);
              }
          
              private Runnable runnable = new Runnable() {
                  @Override
                  public void run() {
                      PrefUtils.setBoolean(BaseActivity.this, "isLogin", false);
                      AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
                      builder.setTitle("温馨提示")
                              .setCancelable(false)
                              .setMessage("当前登录已失效,请重新登录")
                              .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(DialogInterface dialogInterface, int i) {
                                      Intent intent = new Intent();
                                      intent.setClass(BaseActivity.this, LoginActivity.class);
                                      startActivity(intent);
                                  }
                              });
                      AlertDialog alertDialog = builder.create();
                      alertDialog.show();
                  }
              };
          
              public void startAD() {
                  handler.removeCallbacks(runnable);
                  handler.postDelayed(runnable, time);
              }
          
          
              @Override
              protected void onDestroy() {
                  super.onDestroy();
              }
          }
    
  • 如果仅仅是这样是不能满足我们的需求的,例如我从MainActivity 跳到A Actiity的时候我不会将MainActivity销毁,那么当我在A Activity中长时间没有操作跳到登录页面登录之后再重新打开MainActivity的时候就会出问题,这个时候我们需要将所有打开的Activity全部关闭

  • 解决方法:自定义一个Application,定义一个集合用来存放所有Actiity的实例,当我们需要退出的时候调用集合依次执行finish();

  • 下面贴出Application代码:

      /**
       * Created by 一位不愿透露自己姓氏的先生 on 2017/4/24.
       */
      
      public class DemoAppLication extends Application {
          private static DemoAppLication mycontext;
          private ArrayList<Activity> list;
      
          @Override
          public void onCreate() {
              super.onCreate();
              list = new ArrayList<>();
      
          }
      
          public void AddActivity(Activity activity) {
      
              list.add(activity);
      
          }
      
          public void exit() {
              try {
                  for (Activity activity : list) {
                      if (activity != null)
                          activity.finish();
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
      }
    
  • 然后在BaseActivity获取Application的实例并保存代码修改如下:

      /**
       * Created by 一位不愿透露自己姓氏的先生 on 2017/8/23.
       */
      
      public class BaseActivity extends AppCompatActivity {
    
    
      public MiaoHuAppLication application;
    
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          application = (DemoLication) getApplication();
          startAD();
      }
    
      private Handler handler = new Handler();
      private long time = 1000 * 5;
    
           @Override
          public boolean dispatchTouchEvent(MotionEvent event) {
              switch (event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                      handler.removeCallbacks(runnable);
                      break;
                  case MotionEvent.ACTION_UP:
                      startAD();
                      break;
              }
              return super.dispatchTouchEvent(event);
          }
          
      private Runnable runnable = new Runnable() {
          @Override
          public void run() {
              PrefUtils.setBoolean(BaseActivity.this, "isLogin", false);
              AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
              builder.setTitle("温馨提示")
                      .setCancelable(false)
                      .setMessage("当前登录已失效,请重新登录")
                      .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
                              Intent intent = new Intent();
                              intent.setClass(BaseActivity.this, LoginActivity.class);
                              startActivity(intent);
                              IMMessage.stop();
                              application.exit();
                          }
                      });
              AlertDialog alertDialog = builder.create();
              alertDialog.show();
          }
      };
    
      public void startAD() {
          handler.removeCallbacks(runnable);
          handler.postDelayed(runnable, time);
      }
    
    
      @Override
      protected void onDestroy() {
          super.onDestroy();
      }
      }
    
  • 最后在每一个Activity中调用如下代码:

      application.AddActivity(this);
    
  • 然后在需要关闭Activity的地方调用如下代码:

      application.exit();
    

不足之处还请各路大牛指点。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容