如何在x分钟后关闭活动?

时间:2013-04-16 10:59:36

标签: java android

我正在寻找在x分钟后关闭已打开活动的最简单方法。 android是否提供倒计时课程,还是我必须自己制作一个?

我已经尝试过这段代码,但它无效

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();

7 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:4)

永远不会*调用Java run()的{​​{1}}方法。调用其Thread方法,这会导致Java在新线程中为您调用其start()方法

答案 2 :(得分:3)

糟糕的主意:

  

Process.killProcess(Process.myPid());

你应该在UI线程中调用finish()函数。例如,使用runOnUiThread():link

答案 3 :(得分:2)

它无效,因为您正在调用run方法。它不会启动线程。 所以你需要调用start方法来调用线程

  isOnDisplayThread.start();

还要完成线程,你需要调用

        finish() ///method of the Activity class

如果代码在Activity类中,那么只需调用finish()方法

    if (notOnDisplayTime >= 10) {

            finish();
    }

答案 4 :(得分:2)

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));   
                }
        }

答案 5 :(得分:1)

声明定时器并调用finish()。它将在一定时间后关闭您的活动。

  //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);

答案 6 :(得分:1)

只需使用Handler.postDelayed方法。