Auto logout after 15 minutes due to inactivity in

2020-06-04 15:16发布

How to use timer in android for auto logout after 15 minutes due to inactivity of user?

I am using bellow code for this in my 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;
        }

    }

and onclick of login button I have called intent for service.

Intent intent1 = new Intent(getApplicationContext(),
                        AddEditDeleteActivity.class);
                startService(intent1);

Please advice......

This type of error message is shown after 15 mins

This type of error message is shown after 15 mins

7条回答
叼着烟拽天下
2楼-- · 2020-06-04 16:19

you may need to create a BaseActivity class which all the other Activities in your app extend. in that class start your timer task (TimerTask()) in the onUserInteraction method:

override fun onUserInteraction() {
    super.onUserInteraction()
    onUserInteracted()
}

. The onUserInteracted class starts a TimerTaskService which will be an inner class for my case as below:

private fun onUserInteracted() {
     timer?.schedule(TimerTaskService(), 10000)
}

The TimerTaskService class will be asfollows. Please note the run on UI thread in the case you want to display a DialogFragment for an action to be done before login the user out:

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()
    }
}

You will realise i have a stopTimer method which you have to call after the intended action has be envoked, this class just has timer?.cancel() and you may also need to include it in the onStop() method.

NB: this will run in 10 seconds because of the 10000ms

查看更多
登录 后发表回答