Android超时退出功能解决方案

Android超时退出功能解决⽅案
⼀些App鉴于安全性的考虑可能需要⽤户长时间不操作退出登陆界⾯的功能,百度各种⽅法,但是限于本⾝框架原因均⽆法实现
我的App框架是viewPager+fragment,⽽且每个fragment页⾯还嵌套了viewpager+fragment。由于需要禁⽌viewpager的左右滑动,改
⽤点击按钮切换页⾯,所以viewPager⾃定义以便于禁⽌左右滑动,代码如下:
布局⾥引⽤这个ViewPager将禁⽌viewpager的左右滑动。这样如果要实现退出登陆提醒那就这么做。定义⼀个全局变量time获
取当前时间,每次⽤户操作屏幕的时候都更新time,由于禁⽌了左右滑动,所以在主Activity中对viewPager添加点击事件会被阻⽌,为此
只能在viewpager阻⽌滑动事件之前更新time变量。这样监听超时的操作只能通过Service实现。因为Activity⾥⾯⽆论你对viewpager还
是页⾯的布局监听点击事件均会被阻⽌。 Service的⽅法如下: > public class TimeOutService extends Service { private
MessageDialog exitInput; private String TAG = "TimeOutService"; public static boolean isTimeout; private Context mContext;
private Timer mTimer;
@Override
public IBinder onBind(Intent intent) {
仿兔毛纱线return null;
}
@Override
public void 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 = Instence(mContext);
三相混合步进电机
exitInput.setCanceledOnTouchOutside(false);
exitInput.setCustomDialog("长时间未操作,请重新登录", "", "确定");
String packname = PackageName();
PackageManager pm = PackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.SYSTEM_ALERT_WINDOW", packname            if (permission) {
} else {涡轮压缩机
}
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);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
exitInput.show();
}
return false;
}
});
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
StartCommand(intent, flags, startId);
}
铁氧体电感@Override
public void 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中打开对话框的话需要设置为悬浮窗打开⽅式,所以必须添加*** 这项属于特殊权限,6.0以上系统需要特殊处理***

本文发布于:2024-09-23 10:30:07,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/277417.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:操作   需要   退出   事件
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议