I am using a worker thread, as described here but still the code is blocking the UI. If I sleep at the beginning of the run() method, it doesn't block the UI. The thing is, it's a heavy code that runs from the onCreate method, but no matter what I do, I can't make it not block the UI. What am I doing wrong?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
Log.d("asdf", "asdf");
}
}
}).start();
}
You are not using an AsyncTask - which is the control Android uses to do long running processes off the UI thread.
Bottom line, use this and you will have much more success.
Use Handler
to or .runOnUiThread(runnable)
to call your UI thread. You can execute your thread without the Runnable as well, like this:
new Thread(){
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
Log.d("asdf", "asdf");
}
}
}).start();
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.