10秒后关闭活动?

时间:2013-08-07 12:48:27

标签: android eclipse android-intent android-service

我用它来调用另一个活动

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活动?我应该使用一个帖子吗?

4 个答案:

答案 0 :(得分:19)

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

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

答案 1 :(得分:7)

您可以使用以下方法。

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

答案 2 :(得分:2)

答案 3 :(得分:2)

另一种方式就是这样:

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