简体   繁体   中英

how to close activity after x minutes?

I am looking for simplest way to close opened activity after x minutes. Does android provide countdown class or do I have to made one by my self ?

I've tried this code but its not working

Thread isOnDisplayThread = new Thread(new Runnable() {
            @Override
            public void run() {
                Timer mTimer = new Timer();
                mTimer.schedule(new TimerTask() {    
                    @Override
                    public void run() {
                        Log.d(TAG, (isApplicationOnDisplay) ? "Application on display" : "APPLICATION ON BACKGROUND");

                        if (!isApplicationOnDisplay) {
                            notOnDisplayTime++;
                            if (notOnDisplayTime >= 10) {

                                Process.killProcess(Process.myPid());
                            }
                        } else {
                            notOnDisplayTime = 0;
                        }
                    }
                }, 0, 1000);
            }
        });

        isOnDisplayThread.run();
Handler handler = new Handler();
    Runnable r=new Runnable() {
              @Override
              public void run() {
                finish();
              }         
            };
        handler.postDelayed(r, 2000); 

Never ever ever ever ever* call the run() method of a Java Thread . Call its start() method, which causes Java to call its run() method for you within the new thread .

Bad idea:

Process.killProcess(Process.myPid());

You should call finish() function in UI thread. For example, use runOnUiThread(): link

It is not working because you are calling the run method. It will not start the thread. So you need to call the start method to invoke the thread

  isOnDisplayThread.start();

Also to finish off the thread you need to call the

        finish() ///method of the Activity class

If the code is with in the Activity class then just call the finish() method

    if (notOnDisplayTime >= 10) {

            finish();
    }
private final long startTime = 200000;
private final long interval = 1000;
countDownTimer = new MalibuCountDownTimer(startTime, interval);
    countDownTimer.start();
public class MalibuCountDownTimer extends CountDownTimer{
        public MalibuCountDownTimer(long startTime, long interval){
            super(startTime, interval);
        }
            @Override
            public void onFinish(){
                txttimer.setText("Time's up!");                 
                    //timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
            }
            @Override
            public void onTick(long millisUntilFinished) {
                    //text.setText("Time remain:" + millisUntilFinished);
                    //timeElapsed = startTime - millisUntilFinished;
                    String format = String.format("%%0%dd",2);  
                    String seconds = String.format(format, millisUntilFinished/1000 % 60);  
                     String minutes = String.format(format, (millisUntilFinished /(1000*60))%60);  
                     String hours = String.format(format, (millisUntilFinished /(1000*60*60))%24);  
                     String time =  hours + ":" + minutes + ":" + seconds;  
                     txttimer.setText("Time Remaining:" + time);
                            TimeUnit.MILLISECONDS.toMinutes(timeElapsed) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeElapsed)),
                            TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeElapsed)));
                    System.out.println(hms);
                    //text.setText("Time remain:" + h+":"+m+":"+s);
                    //timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));   
                }
        }

Declare the Timer and Call finish() . It will close your activity after certain time.

  //Declare the timer
  Timer t = new Timer();
  //Set the schedule function and rate
  t.schedule(new TimerTask() {

@Override
public void run() {
     finish(); 
}

 },
  //Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
 1000);

只需使用Handler.postDelayed方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM