I want to start an activity and store the start time in a variable, suppose session variable and check every second for a duration of 30 minutes. The moment it elapses 30 minutes the activity will close.
Close an activity after a defined time period
292 Views Asked by user3099225 At
4
There are 4 best solutions below
0
On
You can use following:
int finishTime = 30000;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
YourActivity.this.finish();
}
}, finishTime * 1000);
0
On
You can create Handler for that as below:
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
finish();
}
};
int MINUTES = 30;
handler.postDelayed(runnable, 1000 * 60 * MINUTES);
If you want to remove callbacks of Runnable.
@Override
protected void onDestroy() {
super.onDestroy();
if (handler != null) {
handler.removeCallbacks(runnable);
}
}
Hope it will helps you..
You can use
CountDownTimerIt is not mentioned in the decantation but you may need to consider how to handle the task if it is in the background.