Digging deeper into the Android source code, it looks like this must
have an active MessageQueue or it would throw an exception. In fact,
adding some logging, shows that the Looper is running and the messages
are being posted to the queue:

10-07 10:51:27.809: VERBOSE/Sample(940): Looper{437942d0}
10-07 10:51:27.809: VERBOSE/Sample(940): mRun=true
10-07 10:51:27.809: VERBOSE/Sample(940): mThread=Thread[Instr:
android.test.InstrumentationTestRunner,5,main]
10-07 10:51:27.809: VERBOSE/Sample(940):
mqueue=android.os.messagequ...@437942f0
10-07 10:51:27.820: VERBOSE/Sample(940):   Message 0: { what=0
when=159828381 }
10-07 10:51:27.820: VERBOSE/Sample(940):   Message 1: { what=0
when=159833447 }
10-07 10:51:27.820: VERBOSE/Sample(940):   Message 2: { what=0
when=159838517 }
10-07 10:51:27.820: VERBOSE/Sample(940):   Message 3: { what=0
when=159843689 }
10-07 10:51:27.820: VERBOSE/Sample(940):   Message 4: { what=0
when=159848756 }
10-07 10:51:27.820: VERBOSE/Sample(940): (Total messages: 5)

This is after running five unit tests. All of them get posted, but are
never run.

Therefore, the system is clearly preparing the Looper, assigning the
MessageQueue and posting the messages (Runnables) from the thread.
However, it is never calling loop on the Looper.

Any ideas what I need to do to get this to work? Note that the system
works under an application, it just doesn't work under test.


--
Jeremy Wadsack

On Oct 5, 3:47 pm, Jeremy Wadsack <[email protected]> wrote:
> I have a class that uses a Handler for a timed, asynchronous activity.
> Something like this:
>
> public class SampleClass {
>   private static final long DELAY = 30000;
>   private boolean isRunning = false;
>   private Handler handler = new Handler();
>
>   public start() {
>     if (!isRunning) {
>       isRunning = true;
>       handler.post(new Thread(task));
>     }
>   }
>
>   public stop() {
>     isRunning = false;
>   }
>
>   private Runnable task = new Runnable() {
>     public void run() {
>       if (!isRunning) {
>         return;
>       }
>
>       // Do some tasks
>       handler.postDelayed(new Thread(this), DELAY);
>     }
>   }
>
> }
>
> I am trying to write a unit test (without having to implement an
> activity that instantiates the class) but I can't seem to get the
> items that are posted to the MessageQueue to ever be fired. Inheriting
> from junit.framework.TestCase doesn't work, but then there wouldn't be
> a MessageQueue for the handler, I'd expect (although, there's no
> error, but the Runnable never gets called). I tried inheriting the
> test class from AndroidTestCase and ApplicationTestCase<Application>
> but neither of those works, even though the former is supposed to
> provide a Context and the latter "all the life cycle of an
> application."
>
> Anyone have any pointers?
>
> --
> Jeremy Wadsack

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