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
Use CountDownTimer
When user has stopped any action use
timer.start()
and when user does the action dotimer.cancel()
Use the build-in function called:
onUserInteraction()
like below: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.First Create Application class.
This App Class add into manifest
Then Create BaseActivity Class that is use in whole applications
After that extend Base activity in another activity
I am agree with Girish in above answer. Rash for your convenience i am sharing code with you.
Add the following code in every activity.
I hope this will help
I found it on github https://gist.github.com/dseerapu/b768728b3b4ccf282c7806a3745d0347
Use above code in Activity as below :