开发者

Running off of UIthread without AsyncTask

I have a task that takes about 10 seconds on the UI thread during initialization of my app, I decided to instead put this in a background thread and show a splash screen while this was going on.. I got this working using AsyncTask... however because of AsyncTasks priority being hard coded so low, and no way to increase it that I can find, the task went from taking 10 seconds to finish, to several minutes.

So, the question I have is, how do I run a task on a NON UI thread without AsyncTask? Everything I have tried to so far seems to run on the UI Task which prevents the SplashScreen from appearing until after the task is done.

So how do I create a thread, run it on a non UI thread? I assume there must be an option other than AsyncTask but so far I have not found it... handlers just seem to run on the UI, as do standard threads/runnables. This has to be possible, but I am just not figuring it开发者_如何学JAVA out.


Since you already got the AsyncTask working, maybe you could just bump it's priority from inside doInBackground():

protected Void doInBackground() {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    /* do the work */
}

Also, for the Handler, not exactly sure what the problem was, but you need to create the Handler in the UI thread, explicitly start a different thread to do your work, then use the handler from that new thread to post messages to the UI thread. AsyncTask is nice because it takes care of all this for you.


Simply create a Thread, set any priority you want and start() it. Or use a Runnable and an Executor. Those will not run on the UI thread. If you need to notify the UI when your background work is done, create a Handler before starting the thread and pass it to your Thread/Runnable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜