Android Java中的Sleep() [英] Sleep() in Android Java

查看:947
本文介绍了Android Java中的Sleep()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注本教程在我的电脑中有一个加载屏幕程序.该教程说,我的活动应该使用Sleep()命令进行Sleep()操作,但是它无法将Sleep()识别为函数,并向我提供了一个错误,询问我是否要创建一个称为Sleep()的方法. >

这是代码示例:

public class LoadingScreenActivity extends Activity {

    //Introduce an delay
    private final int WAIT_TIME = 2500;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        System.out.println("LoadingScreenActivity screen started");

        setContentView(R.layout.loading_screen);
        findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);

        new Handler().postDelayed(new Runnable(){ 

            @Override 
            public void run() {

                //Simulating a long running task
                this.Sleep(1000);
                System.out.println("Going to Profile Data");

                /* Create an Intent that will start the ProfileData-Activity. */
                Intent mainIntent = new Intent(LoadingScreenActivity.this,ProfileData.class); 
                LoadingScreenActivity.this.startActivity(mainIntent);
                LoadingScreenActivity.this.finish(); 
            } 
        }, WAIT_TIME);
    }
}

解决方案

您可以使用以下方法之一:

Thread.sleep(timeInMills);

SystemClock.sleep(timeInMills);

SystemClock.sleep(milliseconds)是非常类似于Thread.sleep(milliseconds)的实用程序功能,但是它忽略了InterruptedException.如果您不使用Thread.interrupt(),请使用此函数进行延迟,因为它将保留线程的中断状态.

I am following this tutorial to have a loading screen in my program. The tutorial says my activity should Sleep() using the Sleep() command, however it does not recognize Sleep() as a function and provides me with an error, asking if I would like to create a method called Sleep().

Here is the code sample:

public class LoadingScreenActivity extends Activity {

    //Introduce an delay
    private final int WAIT_TIME = 2500;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        System.out.println("LoadingScreenActivity screen started");

        setContentView(R.layout.loading_screen);
        findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);

        new Handler().postDelayed(new Runnable(){ 

            @Override 
            public void run() {

                //Simulating a long running task
                this.Sleep(1000);
                System.out.println("Going to Profile Data");

                /* Create an Intent that will start the ProfileData-Activity. */
                Intent mainIntent = new Intent(LoadingScreenActivity.this,ProfileData.class); 
                LoadingScreenActivity.this.startActivity(mainIntent);
                LoadingScreenActivity.this.finish(); 
            } 
        }, WAIT_TIME);
    }
}

解决方案

You can use one of the folllowing methods:

Thread.sleep(timeInMills);

or

SystemClock.sleep(timeInMills);

SystemClock.sleep(milliseconds) is a utility function very similar to Thread.sleep(milliseconds), but it ignores InterruptedException. Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread.

这篇关于Android Java中的Sleep()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆