0

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user3099225
  • 413
  • 1
  • 4
  • 14

4 Answers4

1

You can use CountDownTimer

 new CountDownTimer(30000, 1000) { //time in miliseconds

 public void onTick(long millisUntilFinished) {
     view.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("here close your activity");
 }
}.start();

It is not mentioned in the decantation but you may need to consider how to handle the task if it is in the background.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
1

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..

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
0

You can use following:

int finishTime = 30000; 
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        YourActivity.this.finish();
    }
}, finishTime * 1000);
Amirhosein
  • 4,266
  • 4
  • 22
  • 35
0

Use the following code:

function hideActivity(){
  new Handler().postDelayed(new Runnable(){
   public void run(){
    this.finish();
   }
  }, TIME_TAKEN_TO_BE_HIDDEN);
}
olibiaz
  • 2,551
  • 4
  • 29
  • 31
ekibet
  • 176
  • 2
  • 8