开发者

How to Send the Current Thread to Sleep

Thread thred = new Thread();
        thred.run();


    public void run() {
        while (true)
        {

       try {
                Thread.sleep(500)开发者_C百科;
                Toast toast = Toast.makeText(getApplicationContext(), "Sleep Over", 100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Toast toast = Toast.makeText(getApplicationContext(), "Sleep NOT Over", 100);
            }
        }

This code don't work


Thread thred = new Thread()
{ 


    public void run() {
        while (true)
        {

         try {
                sleep(500);
                Toast toast = Toast.makeText(getApplicationContext(), "Sleep Over", 100);
             } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Toast toast = Toast.makeText(getApplicationContext(), "Sleep NOT Over", 100);
             }
        }
    }
};
thred.start();

You need to call the sleep method of the current thread Thread.sleep will call the static sleep method. You need to make sure you are overriding the run method of the thread class with your own run method and then calling thread.start not run.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜