【问题标题】:Close an Activity after 10 seconds?10 秒后关闭 Activity?
【发布时间】:2013-08-07 12:48:26
【问题描述】:

我用它来调用另一个活动

Main.java

 Intent intent = new Intent(this, Message_Note.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);

Message_Note.java:

public class Message_Note extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message);
    }



}

如何在 10 秒后关闭 Message_Note 活动?我应该使用线程?

【问题讨论】:

    标签: android eclipse android-intent android-service


    【解决方案1】:

    100 毫秒后,活动将使用以下代码完成。

    public class Message_Note extends Activity 
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.message);
    
            Handler handler = new Handler();
    
            handler.postDelayed(new Runnable() {
                public void run() {
                    finish();
                }
            }, 100);
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用以下方法。

      方法 1

      int finishTime = 10; //10 secs
      Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
          public void run() {
              YourActivity.this.finish();
          }
      }, finishTime * 1000);
      

      方法2

      int FinishTime = 10;
      int countDownInterval = 1000; 
      counterTimer = new CountDownTimer(FinishTime * 1000, countDownInterval) {
          public void onFinish() {
              //finish your activity here
          }
      
          public void onTick(long millisUntilFinished) {
              //called every 1 sec coz countDownInterval = 1000 (1 sec)
          }
      };
      counterTimer.start();
      

      【讨论】:

        【解决方案3】:

        【讨论】:

          【解决方案4】:

          另一种方式是这样的:

          new Handler().postDelayed(new Runnable(){
                  @Override
                  public void run() {
                      Message_Note.this.finish();
                  }
              }, 10000);
          

          【讨论】:

            猜你喜欢
            • 2011-05-29
            • 2017-05-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-20
            • 2013-02-10
            相关资源
            最近更新 更多