简体   繁体   中英

Android shows blank screen with System.currentTimeMillis()

I have the following piece of code in my android application. Whenever these lines of code run in my application, the mobile screen goes blank for 10 seconds. This 'problem' is most probably caused by 'System.currentTimeMillis()'


        for (i=0;i<10;i++)
        {
            time0 = System.currentTimeMillis();
            do
            {
            time1 = System.currentTimeMillis();
            }
            while ((time1 - time0) < 1000);
        } 

Is there any way that the blank screen can be avoided?

Thanks

The reason you are getting a blank screen for 10 seconds is because this section of code is blocking the UI thread. It has nothing to do with the call to System.currentTimeMillis() .

On each for loop iteration, you are checking the time over and over until 1 second has passed. Since you execute the for loop 10 times, you are effectively blocking the UI thread for 10 seconds.

EDIT: If you would like to pause execution for a few seconds, take a look at the Java Timer Class

This is probably happening because you are running that piece of code in the UI thread. In order to avoid the blank screen try running this in a separate thread.

The easiest way to do it is using an AsyncTask. See http://developer.android.com/reference/android/os/AsyncTask.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM