如何使用定时器在android系统自动注销15分钟后因用户不活动?
我在我的loginActivity.java使用波纹管代码此
public class BackgroundProcessingService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
timer = new CountDownTimer(5 *60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
//inactivity = true;
timer.start();
Log.v("Timer::", "Started");
}
public void onFinish() {
//Logout
Intent intent = new Intent(LoginActivity.this,HomePageActivity.class);
startActivity(intent);
//inactivity = false;
timer.cancel();
Log.v("Timer::", "Stoped");
}
};
return null;
}
}
和的onclick登录按钮的我呼吁服务的意图。
Intent intent1 = new Intent(getApplicationContext(),
AddEditDeleteActivity.class);
startService(intent1);
请指教......
此类型的错误消息被后15分钟显示
Answer 1:
使用CountDownTimer
CountDownTimer timer = new CountDownTimer(15 *60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
}
public void onFinish() {
//Logout
}
};
当用户停止任何动作用timer.start()
当用户执行的动作做timer.cancel()
Answer 2:
我在上面的回答与吉里什同意。 皮疹为您提供方便我与你共享代码。
public class LogoutService extends Service {
public static CountDownTimer timer;
@Override
public void onCreate(){
super.onCreate();
timer = new CountDownTimer(1 *60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
Log.v(Constants.TAG, "Service Started");
}
public void onFinish() {
Log.v(Constants.TAG, "Call Logout by Service");
// Code for Logout
stopSelf();
}
};
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
添加以下代码在每一个活动。
@Override
protected void onResume() {
super.onResume();
LogoutService.timer.start();
}
@Override
protected void onStop() {
super.onStop();
LogoutService.timer.cancel();
}
Answer 3:
您可以启动一个服务,它启动一个定时器。 每隔15分钟,检查是否有标志,假设inactivity
标志设置为true。 如果是,注销表单的应用程序。
每个用户与您的应用程序交互时,设置inactivity
标志设置为false。
Answer 4:
您可能需要创建一个BaseActivity类,在你的应用程序中的所有其他活动扩展。 在该类中onUserInteraction方法启动计时器任务(TimerTask的()):
override fun onUserInteraction() {
super.onUserInteraction()
onUserInteracted()
}
。 该onUserInteracted类开始,这将是下面我的情况的内部类TimerTaskService:
private fun onUserInteracted() {
timer?.schedule(TimerTaskService(), 10000)
}
该TimerTaskService类将是asfollows。 请注意,在情况下,你要显示被之前完成的动作登录用户了DialogFragment在UI线程运行:
inner class TimerTaskService : TimerTask() {
override fun run() {
/**This will only run when application is in background
* it allows the application process to get high priority for the user to take action
* on the application auto Logout
* */
// val activityManager = applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
// activityManager.moveTaskToFront(taskId, ActivityManager.MOVE_TASK_NO_USER_ACTION)
runOnUiThread {
displayFragment(AutoLogoutDialogFragment())
isSessionExpired = true
}
stopLoginTimer()
}
}
你会意识到我有,你要打电话预定动作已被envoked后,这个类只是有一个stopTimer方法timer?.cancel()
您可能还需要包括它在onStop()
方法。
注:这会因为10000ms 10秒运行
Answer 5:
我希望这个能帮上忙
我发现它在GitHub上https://gist.github.com/dseerapu/b768728b3b4ccf282c7806a3745d0347
public class LogOutTimerUtil {
public interface LogOutListener {
void doLogout();
}
static Timer longTimer;
static final int LOGOUT_TIME = 600000; // delay in milliseconds i.e. 5 min = 300000 ms or use timeout argument
public static synchronized void startLogoutTimer(final Context context, final LogOutListener logOutListener) {
if (longTimer != null) {
longTimer.cancel();
longTimer = null;
}
if (longTimer == null) {
longTimer = new Timer();
longTimer.schedule(new TimerTask() {
public void run() {
cancel();
longTimer = null;
try {
boolean foreGround = new ForegroundCheckTask().execute(context).get();
if (foreGround) {
logOutListener.doLogout();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}, LOGOUT_TIME);
}
}
public static synchronized void stopLogoutTimer() {
if (longTimer != null) {
longTimer.cancel();
longTimer = null;
}
}
static class ForegroundCheckTask extends AsyncTask < Context, Void, Boolean > {
@Override
protected Boolean doInBackground(Context...params) {
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List < ActivityManager.RunningAppProcessInfo > appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess: appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}
}
使用上面在如下活动代码:
public class MainActivity extends AppCompatActivity implements LogOutTimerUtil.LogOutListener
{
@Override
protected void onStart() {
super.onStart();
LogOutTimerUtil.startLogoutTimer(this, this);
Log.e(TAG, "OnStart () &&& Starting timer");
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
LogOutTimerUtil.startLogoutTimer(this, this);
Log.e(TAG, "User interacting with screen");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause()");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume()");
}
/**
* Performing idle time logout
*/
@Override
public void doLogout() {
// write your stuff here
}
}
Answer 6:
利用处理器,用户交互()和运行的类的。
CODE:MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
// manageBackup(true,false);
logout();
}
};
startHandler(); //will start the timer. This must be withing onCreate() scope.
//代码不活动的部分。
@Override
public void onUserInteraction() {
super.onUserInteraction();
stopHandler(); //first stop the timer and then again start it
startHandler();
}
public void stopHandler() {
handler.removeCallbacks(runnable);
}
public void startHandler() {
handler.postDelayed(runnable, 15*60*1000); //for 15 minutes
}
文章来源: Auto logout after 15 minutes due to inactivity in android