Auto Logout After 15 Minutes Due To Inactivity In Android
Solution 1:
Use CountDownTimer
CountDownTimer timer = new CountDownTimer(15 *60 * 1000, 1000) {
publicvoidonTick(long millisUntilFinished) {
//Some code
}
publicvoidonFinish() {
//Logout
}
};
When user has stopped any action use timer.start()
and when user does the action do timer.cancel()
Solution 2:
I am agree with Girish in above answer. Rash for your convenience i am sharing code with you.
publicclassLogoutServiceextendsService {
publicstaticCountDownTimer timer;
@OverridepublicvoidonCreate(){
super.onCreate();
timer = newCountDownTimer(1 *60 * 1000, 1000) {
publicvoidonTick(long millisUntilFinished) {
//Some codeLog.v(Constants.TAG, "Service Started");
}
publicvoidonFinish() {
Log.v(Constants.TAG, "Call Logout by Service");
// Code for LogoutstopSelf();
}
};
}
@Overridepublic IBinder onBind(Intent intent) {
returnnull;
}
}
Add the following code in every activity.
@OverrideprotectedvoidonResume() {
super.onResume();
LogoutService.timer.start();
}
@OverrideprotectedvoidonStop() {
super.onStop();
LogoutService.timer.cancel();
}
Solution 3:
First Create Application class.
publicclassAppextendsApplication{
privatestaticLogoutListener logoutListener = null;
privatestaticTimer timer = null;
@OverridepublicvoidonCreate() {
super.onCreate();
}
publicstaticvoiduserSessionStart() {
if (timer != null) {
timer.cancel();
}
timer = newTimer();
timer.schedule(newTimerTask() {
@Overridepublicvoidrun() {
if (logoutListener != null) {
logoutListener.onSessionLogout();
log.d("App", "Session Destroyed");
}
}
}, (1000 * 60 * 2) );
}
publicstaticvoidresetSession() {
userSessionStart();
}
publicstaticvoidregisterSessionListener(LogoutListener listener) {
logoutListener = listener;
}
}
This App Class add into manifest
<application
android:name=".App"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity android:name=".view.activity.MainActivity"/>
</application>
Then Create BaseActivity Class that is use in whole applications
classBaseActivityextendsAppCompatActivityimplementsLogoutListener{
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
//setTheme(App.getApplicationTheme());super.onCreate(savedInstanceState);
}
@OverrideprotectedvoidonResume() {
super.onResume();
//Set Listener to receive eventsApp.registerSessionListener(this);
}
@OverridepublicvoidonUserInteraction() {
super.onUserInteraction();
//reset session when user interactApp.resetSession();
}
@OverridepublicvoidonSessionLogout() {
// Do You Task on session out
}
}
After that extend Base activity in another activity
publicclassMainActivityextendsBaseActivity{
privatestaticfinalStringTAG= MainActivity.class.getSimpleName();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Solution 4:
You can start a service and start a timer in it. Every 15 minutes, check if a flag, let's say inactivity
flag is set to true. If it is, logout form the app.
Every time the user interacts with your app, set the inactivity
flag to false.
Solution 5:
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:
overridefunonUserInteraction() {
super.onUserInteraction()
onUserInteracted()
}
. The onUserInteracted class starts a TimerTaskService which will be an inner class for my case as below:
privatefunonUserInteracted() {
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:
innerclassTimerTaskService : TimerTask() {
overridefunrun() {
/**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
Post a Comment for "Auto Logout After 15 Minutes Due To Inactivity In Android"