在一段时间后杀死一个活动

时间:2014-03-29 05:49:54

标签: android timer runnable

我在测验应用程序上工作。我有不同类型的测验。 Quiz with Time and Quiz without Time。没有时间的测验很简单,没有确定或特定的时间来尝试测验。但是时间测验是限时测验。

What I Want:

当个人尝试尝试限时测验时。将在活动上显示一定量的时间,例如剩余时间(剩余30分钟)。在 30th min 之后,活动会自动停止。我尝试过 timer class ,但这用于延迟。

如何在一段时间后停止活动?

先谢谢

4 个答案:

答案 0 :(得分:2)

    public void countDown() {
    new CountDownTimer(60000*30, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) (millisUntilFinished / 1000) % 60 ;
            int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
            int hours   = (int) ((millisUntilFinished / (1000*60*60)) % 24);                
            String my_new_str =((((((((( (pad(hours)+":"+pad(minutes)+":"+pad(seconds)));
            timer.setText( my_new_str);     
            float pvalue =  ( float)(millisUntilFinished*100)/(60000*30);               
            myprogressbar.setProgress((int)(Math.round(pvalue)));
        }
        public void onFinish() {
            timer.setText("Completed");
            goToResult();
            this,finish();
        }
    }.start();
}

答案 1 :(得分:1)

您可以使用处理程序

执行此操作

尝试这样的事情......

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

答案 2 :(得分:0)

将您的问题放在Thread ...

Thread t=new Thread()
        {
            public void run()
            {
                try{
                    sleep(30000);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    Intent intent = new Intent(Splash.this, Login.class);
                    startActivity(intent);
                    finish();
                }
            }
        };
        t.start();

答案 3 :(得分:0)

    Try to use AsyncTask or Handler in your Activity. i think you set some certain time limits for the 
    Quiz Question so please make sure to use any one of this above i mentioned.

    Handler handler = new Handler();
    Runnable x=new Runnable() {
              @Override
              public void run() {
                   Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
                  startActivity(intent);
                finish();// activity will kill. or use 
                System.Exit();
              }         
            };
        handler.postDelayed(x, yourtimelimit(in timemills or millisecond)); 


  Even you doesn't work yet means try to use onComplete listener and the put it finish in that method or 
use onDestroy method inside to finish().
    thank you