Kind of off-topic, but why are you creating a new Thread with each
post, vs simply posting the Runnable?

On Oct 5, 5: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