开发者

Android Thread makes the UI blank screen

开发者 https://www.devze.com 2023-02-24 08:20 出处:网络
I have written a Thread in my application. When the thread starts running, the UI screen becomes blank. How to can I avoid this?

I have written a Thread in my application. When the thread starts running, the UI screen becomes blank. How to can I avoid this?

public class SetTickerText extends Thread
{
    @Override
    public void run()
    {
        while(true)
        {
            SystemClock.sleep(25000)开发者_开发百科;
            Log.i("Map", "after wait");
        }
    }


It's going to be something like (don't have a compiler near me):

new SetTickerText().start()

Essentially, when you tell the thread object to start, it spins up the new thread, which then invokes run for you. What you're doing is calling run from the UI thread, just like any other function, so it's blocking your UI thread from returning


You main problem is using SystemClock.sleep. This method will force the CPU going into deep thead. What you want to do is to sleep the current thread which is done using Thread.sleep: http://developer.android.com/reference/java/lang/Thread.html#sleep(long).


For me, the problem was that I have called

thread.run()

instead of

thread.start()

by mistake.

For more details follow,

https://beginnersbook.com/2015/03/why-dont-we-call-run-method-directly-why-call-start-method/

0

精彩评论

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

关注公众号