0

我正在寻找在 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();
4

7 回答 7

5
Handler handler = new Handler();
    Runnable r=new Runnable() {
              @Override
              public void run() {
                finish();
              }         
            };
        handler.postDelayed(r, 2000); 
于 2013-04-16T11:04:34.103 回答
4

永远永远永远*调用run()Java 的方法Thread。调用它的start()方法,这会导致 Java在新线程run()中为您调用它的方法。

于 2013-04-16T11:01:50.780 回答
3

馊主意:

Process.killProcess(Process.myPid());

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

于 2013-04-16T11:01:47.050 回答
2

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

  isOnDisplayThread.start();

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

        finish() ///method of the Activity class

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

    if (notOnDisplayTime >= 10) {

            finish();
    }
于 2013-04-16T11:03:59.277 回答
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));   
                }
        }
于 2013-04-16T11:14:08.093 回答
1

简单的使用Handler.postDelayed方法。

于 2013-04-16T11:04:19.477 回答
1

声明 Timer 和 Call 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);
于 2013-04-16T11:03:01.287 回答