功能描述:
APP在公共的设备上运行,出于安全考虑,当登录的用户在超过一定时间内没有做任何操作,
则系统自动登出,用户如需重新操作APP,则需要重新登录 。
第一步:创建一个BaseActivity
/** * 创建时间: 2019-02-26 * 创建人:陈松 * 说明: */ public class BaseActivity extends FinalActivity { public Context context; //运行上下文 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); startAD(); } private Handler handler = new Handler(); private long time = 1000 * 60 * 5; //设置限时时间: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() { //超时要执行的内容(自定义),此片弹窗提示,并跳转页面 new ShowDialog(context, R.style.dialog, "当前登录已失效,请重新登录!", new ShowDialog.OnCloseListener() { @Override public void onClick(Dialog dialog, boolean confirm) { Intent intent=new Intent(context, LoginActivity.class); startActivity(intent); finish(); } }).setTitle("温馨提示").show(); } }; public void startAD() { handler.removeCallbacks(runnable); handler.postDelayed(runnable, time); } @Override protected void onDestroy() { super.onDestroy(); } //endregion }
第二步:在主操作的Activity中extends BaseActivity
public class MenuActivity extends BaseActivity { }
第三步:在MenuActivity的onCreate方法中,给BaseActivity 的context属性赋值
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_menu_main); context=MenuActivity.this; }
完毕收工~~~~~