If you use Android's JUnit test runner, you can use the @OnUiThread
annotation to run a test on the UI thread :)

On Mon, Aug 9, 2010 at 8:55 AM, Streets Of Boston
<[email protected]> wrote:
> I had a similar problem. The problem i had was in JUnit tests, which
> run in non-UI threads, causing AsyncTasks to be loaded in non-UI
> threads and i got the same error message. However, as long as you
> create your process' first AsyncTask in a UI-thread, all next ones can
> be created in non-UI threads.
>
> This is what i did in my JUnit tests:
>
> First some 'static' code calls this method below as soon as possible,
> which creates a dummy AsyncTask that is never executed. It just makes
> sure that the Looper for every AsyncTask is created in a UI-thread.
> public static synchronized void initialize(Instrumentation inst) {
>        if (hasBeenInitialized)
>                return;
>
>        // Just to load AsyncTask in the main UI thread....
>        // If the first AsyncTask instance is loaded in any other thread, UI-
> callbacks will never work.
>        inst.runOnMainSync(new Runnable() {
>                public void run() {
>                        Object dummy = new AsyncTask<Void,Void,Void>() { 
> protected Void
> doInBackground(Void... params) { return null; } };
>                }
>        });
>        hasBeenInitialized = true;
> }
>
> After this method has been called, AsyncTasks can be created from non-
> UI threads.
>
> If you don't deal with JUnit tests and such, just make sure that your
> first (dummy) AsyncTask instance is created in a UI-thread (e.g
> onCreate). All subsequent ones can be created in non-UI threads.
>
> However, TreKing's comments about calling performTask when your
> service starts is still very valid :-)
>
>
> On Aug 9, 7:06 am, Samuh <[email protected]> wrote:
>> I have a helper class that contains definition of an AsyncTask(inner
>> class). Other classes instantiate this class and call one of its
>> method which in turn instantiates, the AsyncTask and starts execution.
>>
>> class TaskManager{
>>
>> private class SomeTask extends AsyncTask<>{}
>>
>> public void performTask(){
>> new SomeTask().execute();
>>
>> }
>> }
>>
>> //usage: new TaskManager().performTask();
>>
>> I schedule Alarms which when triggered start a Service. When the
>> service starts it creates a Runnable and enqueues it in the work
>> queue. From this Runnable, I want to instantiate my helper class and
>> start execution of the AsynTask.
>>
>> The problem is AsyncTask needs to be instantiated from main UI thread
>> I am getting errors as soon as the spawned thread calls the method of
>> the helper class. The exception is that AsyncTask cannot be called
>> from a thread that hasnt called Looper.prepare().
>>
>> How do I avoid this?
>
> --
> 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
>



-- 
Romain Guy
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

-- 
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

Reply via email to