一些App鉴于安全性的考虑可能需要用户长时间不操作退出登陆界面的功能,百度各种方法,但是限于本身框架原因均无法实现
我的App框架是viewPager+fragment,而且每个fragment页面还嵌套了viewpager+fragment。由于需要禁止viewpager的左右滑动,改用点击按钮切换页面,所以viewPager自定义以便于禁止左右滑动,代码如下:
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
LogUtil.w(TAG, "onCreate");
mContext = this;
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if ((System.currentTimeMillis() - FaisAppConfig.time) > 3 * 60 * 1000) {
Message msg = new Message();
msg.what = 0x1234;
handler.sendMessage(msg);
}
}
}, 0, 2000);
}
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
LogUtil.w(TAG, "长时间未操作");
if (HeartService.isShowing)
LogUtil.w(TAG, "已经有对话框打开");
if (message.what == 0x1234 && !HeartService.isShowing) {
LogUtil.w(TAG, "打开对话框");
mTimer.cancel();
exitInput = MessageDialog.getInstence(mContext);
exitInput.setCanceledOnTouchOutside(false);
exitInput.setCustomDialog("长时间未操作,请重新登录", "", "确定");
String packname = mContext.getPackageName();
PackageManager pm = mContext.getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.SYSTEM_ALERT_WINDOW", packname));
if (permission) {
exitInput.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
} else {
exitInput.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
}
exitInput.setOnPositiveListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exitInput.dismiss();
stopSelf();
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
exitInput.show();
}
return false;
}
});
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
FaisAppConfig.time = System.currentTimeMillis();
exitInput = null;
if (null != mTimer) {
mTimer.cancel();
mTimer = null;
}
Intent i = new Intent();
i.setClass(TimeOutService.this, HeartService.class);
stopService(i);
LogUtil.w(TAG, "onDestroy");
}
}
复制代码
另外在Service中打开对话框的话需要设置为悬浮窗打开方式,所以必须添加***SYSTEM_ALERT_WINDOW 这项属于特殊权限,6.0以上系统需要特殊处理***