10

我用它来调用另一个活动

主.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

4 回答 4

26

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);
    }
}
于 2013-08-07T12:59:12.233 回答
7

您可以使用以下方法。

方法一

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

方法二

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();
于 2013-08-07T12:59:22.813 回答
2

您可以使用警报管理器。看 :

http://developer.android.com/reference/android/app/AlarmManager.html

警报管理器示例

于 2013-08-07T12:51:13.630 回答
2

另一种方式是这样的:

new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            Message_Note.this.finish();
        }
    }, 10000);
于 2013-08-07T13:03:08.933 回答