Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在x分钟后关闭活动?_Java_Android - Fatal编程技术网

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

Java 如何在x分钟后关闭活动?,java,android,Java,Android,我正在寻找在x分钟后关闭已打开活动的最简单方法。android是否提供倒计时类,还是我必须自己制作一个 我尝试过这个代码,但它不起作用 Thread isOnDisplayThread = new Thread(new Runnable() { @Override public void run() { Timer mTimer = new Timer(); mTimer.schedu

我正在寻找在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();
坏主意:

Process.killProcess(Process.myPid())


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

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

声明计时器并调用
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);

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

  isOnDisplayThread.start();
此外,为了完成线程,您需要调用

        finish() ///method of the Activity class
如果代码位于Activity类中,则只需调用finish()方法


只需使用
Handler.postDelayed
方法。

什么是“它不工作”意思?这个问题很容易改进。请更正。我确实喜欢你的代码简单而专业,祝福你;)@西德哈特,谢谢你,伙计!我以后会照顾你的。
Handler handler = new Handler();
    Runnable r=new Runnable() {
              @Override
              public void run() {
                finish();
              }         
            };
        handler.postDelayed(r, 2000); 
Handler handler = new Handler();
    Runnable r=new Runnable() {
              @Override
              public void run() {
                finish();
              }         
            };
        handler.postDelayed(r, 2000); 
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));   
                }
        }