項目中遇到一個需求撤嫩,程序在一段時間沒有操作的情況下退出登錄女责,以此作為隨筆記憶膘流。
原理:在BaseActivity中重寫dispatchTouchEvent方法盔夜,在MotionEvent.ACTION_UP事件中利用Handler發(fā)送一個延時消息,在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("當(dāng)前登錄已失效,請重新登錄") .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銷毀肛捍,那么當(dāng)我在A Activity中長時間沒有操作跳到登錄頁面登錄之后再重新打開MainActivity的時候就會出問題隐绵,這個時候我們需要將所有打開的Activity全部關(guān)閉
解決方法:自定義一個Application,定義一個集合用來存放所有Actiity的實例,當(dāng)我們需要退出的時候調(diào)用集合依次執(zhí)行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("當(dāng)前登錄已失效依许,請重新登錄") .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中調(diào)用如下代碼:
application.AddActivity(this);
-
然后在需要關(guān)閉Activity的地方調(diào)用如下代碼:
application.exit();