Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto logout after 15 minutes due to inactivity in android

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

like image 287
Rash Avatar asked Nov 06 '12 05:11

Rash


People also ask

How do I get Android to automatically logout at a certain time everyday?

just follow this, create an broadcast receiver, register to receive an intent with action say ACTION_CLEAR_SESSION. register an pending intent of type boradcast, targetting your receiver with alarm manager. set the repeat mode as daily, and set the trigger time just before midnight.

How do I stay logged in Android Studio?

When users log in to your application, store the login status into sharedPreference and clear sharedPreference when users log out. Check every time when the user enters into the application if user status from shared Preference is true then no need to log in again otherwise direct to the login page.

How do I log off my computer after inactivity?

Step 1: Right click on the desktop, and select Personalize option. Step 2: From the left side panel click on Lock Screen and select Screen saver settings. Step 3: From the drop down bar under screen saver select an option. Step 4: Check the box On resume, display logon screen, change the number to 5 in the Wait box.


1 Answers

Use CountDownTimer

CountDownTimer timer = new CountDownTimer(15 *60 * 1000, 1000) {

        public void onTick(long millisUntilFinished) {
           //Some code
        }

        public void onFinish() {
           //Logout
        }
     };

When user has stopped any action use timer.start() and when user does the action do timer.cancel()

like image 133
Girish Nair Avatar answered Sep 30 '22 18:09

Girish Nair