I found out that you can create AsyncTask instances on any thread, as long as your code initially loads the class on the main UI thread (as you stated in your message, Bob).
I hit upon this issue when writing JUnit tests that were testing AsyncTask subclasses in my code. JUnit tests don't run on the main UI thread. Creating just one dummy AsyncTask on the main UI thread before running the tests, let my JUnit tests run fine. On Dec 31 2010, 1:50 am, Bob Kerns <[email protected]> wrote: > You need to create the AsyncTask instance on the UI thread, but the > doInBackground() method runs in a separate thread. > > The reason for creating the AsyncTask instance on the UI thread is so > that it can connect to the proper Looper and MessageQueue for that > thread. (It does this through a single static Handler, so it will not > work correctly if you first load the class in a different thread. > That's unnecessarily fragile, in my opinion, but not a problem if you > are using it as intended, as an easy way to move code off the main > thread.) > > On Dec 30, 9:39 pm, water boiled <[email protected]> wrote: > > > > > I mean that can i create AsycnTask instance not in UI thread? > > > On 12月31日, 下午1时30分, water boiled <[email protected]> wrote: > > > > I mean that can i AsycnTask instance not in UI thread? > > > > On 12月31日, 下午1时24分, water boiled <[email protected]> wrote: > > > > > the AsycnTask document says that:The task instance must be created on > > > > the UI thread. > > > > > why create task instance must be created in UI thread? > > > > > On 12月29日, 上午10时26分, Frank Weiss <[email protected]> wrote: > > > > > > It's difficult to understand what you are asking. What problem are you > > > > > having with AsyncTask.- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

