0

here is my code

public class LogoutService extends Service {
    public static CountDownTimer timer;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        timer = new CountDownTimer(1 * 60 * 1000, 1000) {
            public void onTick(long millisUntilFinished) {
                //Some code
                Log.v("Timer::", "Service Started");
            }

            public void onFinish() {
                Log.v("Timer::", "Call Logout by Service");
                // Code for Logout
                stopSelf();
            }
        };
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
Aakarsh Rai
  • 1
  • 1
  • 7

3 Answers3

3

Create a BaseActivity class that needs to be extended by all activities.

public class MyBaseActivity extends Activity {

    public static final long DISCONNECT_TIMEOUT = 900000; // 15 min = 15 * 60 * 1000 ms

    private Handler disconnectHandler = new Handler(){
        public void handleMessage(Message msg) {
        }
    };

    private Runnable disconnectCallback = new Runnable() {
        @Override
        public void run() {
            // Perform any required operation on disconnect  

           // Logout from app
        }
    };

    public void resetDisconnectTimer(){
        disconnectHandler.removeCallbacks(disconnectCallback);
        disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
    }

    public void stopDisconnectTimer(){
        disconnectHandler.removeCallbacks(disconnectCallback);
    }

    @Override
    public void onUserInteraction(){
        resetDisconnectTimer();
    }

    @Override
    public void onResume() {
        super.onResume();
        resetDisconnectTimer();
    }

    @Override
    public void onStop() {
        super.onStop();
        stopDisconnectTimer();
    }
}

In Other way this also will work.

public class MyBaseActivity extends Activity {

   public static final int IDLE_DELAY_MINUTES = 15; // 15 min 
        @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        delayedIdle(IDLE_DELAY_MINUTES);
    }

    Handler _idleHandler = new Handler();
    Runnable _idleRunnable = new Runnable() {
        @Override
        public void run() {
            //handle your IDLE state

      // Logout from app
        }
    };

    private void delayedIdle(int delayMinutes) {
        _idleHandler.removeCallbacks(_idleRunnable);
        _idleHandler.postDelayed(_idleRunnable, (delayMinutes * 1000 * 60));
    }

        }
King of Masses
  • 18,405
  • 4
  • 60
  • 77
2

You can find user interaction using default method:

@Override public void onUserInteraction() {
 super.onUserInteraction(); 
}

check user interaction using this method and if no interaction found after your time limit, call logout method.

You can find more information using How to detect USER INACTIVITY in android

Nik
  • 1,991
  • 2
  • 13
  • 30
0

You should use Handler for that. Use it like

 android.os.Handler handler = new android.os.Handler(); //declear them globly
        Runnable runnable=null;

Initialize Runnable inside onCreate() like

runnable = new Runnable() {
            @Override
            public void run() {
            finish();
            }
        };

Copy these two methods in your code

void start() {
    handler.postDelayed(runnable, TimeUnit.MINUTES.toMillis(15));
}
void stop(){
    handler.removeCallbacks(runnable);
}

Override onUserInteraction() method of Activityand call start() and stop()

 @Override
public void onUserInteraction() {
    super.onUserInteraction();
    stop(); 
    start();

}

Don't forget to call start() method from onCreate() too after the runnable initialization. Its run() method will call just after 15 minutes after user inactive, do your task there(you want to finish that Activity or something else)

//////////=====Updated=====\\\\\\\\\

Paste this BaseActivity in your code

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.concurrent.TimeUnit;

public class BaseActivity extends AppCompatActivity {
    private static android.os.Handler handler = new android.os.Handler();
    private static Runnable runnable = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (handler == null) {
            handler = new Handler();
        } else {
            handler.removeCallbacks(runnable);
        }
        if (runnable == null)
            runnable = new Runnable() {
                @Override
                public void run() {
              //do your task here
                }
            };
        start();
    }

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        stop();
        start();

    }

    void start() {
        handler.postDelayed(runnable, TimeUnit.MINUTES.toMillis(15));
    }

    void stop() {
        handler.removeCallbacks(runnable);
    }

}

Now extends BaseActivity from all of your Activity except AppCompatActivity or else, like this.

public class HomeActivity extends BaseActivity{
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }
}

Do you task inside run() method, I suggest you to start another Activity and clear activity stack from run() method, because finish() will work there only for 1 Activity.

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41