Android中的计时器无法正常工作

时间:2010-08-15 00:18:12

标签: android timer

我正在尝试在Android上启动应用程序后5秒钟显示警告框。但它没有用,我不知道为什么。这是代码:

package com.example.helloandroid;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;



public class HelloAndroid extends Activity {

public void showalert() {
    /* alert box--------------------------- */
    AlertDialog dialog=new AlertDialog.Builder(HelloAndroid.this).create();
    dialog.setTitle("Test");
    dialog.setMessage("Test");
    dialog.setButton("Test 1",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.setButton2("Test 2",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.setButton3("Test 3",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.show();
    /* -------------------------------------- */
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);


    new Timer().schedule(new TimerTask()
    { 
        public void run() 
        { 
            showalert();
        } 
    }, 5000);

    setContentView(R.layout.main);

}

}

谁能看到我做错了什么?为什么警报不显示......?谢谢你的帮助!

最好的, 迈克尔

编辑(这是经过一些修改后的工作代码):

package com.example.helloandroid;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;



public class HelloAndroid extends Activity {

    public void showalert() {
        /* alert box--------------------------- */
        AlertDialog dialog=new AlertDialog.Builder(HelloAndroid.this).create();
        dialog.setTitle("Test");
        dialog.setMessage("Test");
        dialog.setButton("Test 1",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.setButton2("Test 2",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.setButton3("Test 3",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.show();
        /* -------------------------------------- */
    }

    public final void timerAlert(){ 

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                showalert();
            }
        }, 5000);

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        timerAlert();
    }
}

3 个答案:

答案 0 :(得分:3)

您也可以在Handler中使用postDelayed()方法

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        showalert();
    }
}, 5000);

http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29

答案 1 :(得分:0)

试试这个(让你的showalert函数离开):

    public final void timerAlert(){ 

      t = new Timer();
      tt = new TimerTask() {

          @Override //Override!!
          public void run() {
              showalert();
              t.purge();
          }

      };
      t.schedule(tt, 5000);

}

首先,定义上面的函数,然后在onCreate中放置timerAlert();

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

timerAlert();

}

答案 2 :(得分:0)

第一种方式

new Timer().schedule(new TimerTask()
{
@Override 
   public void run()
   {
   // your action hear
   }
}, timeToExecute);

这会在timeToExecute秒后执行您的操作。它就像一个魅力,不要忘记@Override

使用handle.postDelayed在“编辑”中发布的第二种方式。