Thanks for the response Romain.  I think I'm getting it.

I have one other follow up though if you know anything special about
this.

Just as an experiment.  I tried changing this line (detailed in the
original post):

mRedrawHandler.sleep(mMoveDelay);

to this:

postDelayed((new Runnable(){
                        public void run(){
                                SnakeView.this.update();
                                SnakeView.this.invalidate();
                        }
                    }), mMoveDelay);

When I run the Snake program using the postDelayed method, Snake runs
noticeably choppier.  Would you have any suspicions on why that would
be?

I've only run this on the emulator and not the actual hardware btw.

On Feb 28, 7:46 pm, Romain Guy <romain...@google.com> wrote:
> Hi,
>
> Using a sleep() would be a very bad idea. This method pauses the
> current running thread, in this case the UI thread. If you did so, you
> would block all events dispatching (keys + touch) and drawing.
>
> A Handler is usually used to communicate with the UI thread from
> another thread or to postpone actions on the UI thread, as done in
> this example. Since this example was written, new APIs were added to
> do this kind of thing without creating a custom Handler. For instance,
> you can call View.postInvalidateDelayed() or View.post(Runnable).
> These methods use a Handler behind the scene.
>
>
>
> On Fri, Feb 27, 2009 at 10:47 AM, Sen <senecajust...@gmail.com> wrote:
>
> > Hey all,
> >  I'm really trying to wrap my head around the idea of a Handler and
> > how they work exactly.  When I would use one etc.
>
> > I don't exactly expect a full lesson here but I do have a question
> > about the Snake program that comes with the SDK.  It creates a class
> > called RefreshHandler that extends Handler and is responsible for
> > initiating the next frame or next process in the snake program.  Here
> > is a snippet of code from SnakeView.java
>
> > private RefreshHandler mRedrawHandler = new RefreshHandler();
>
> >    class RefreshHandler extends Handler {
>
> >       �...@override
> >        public void handleMessage(Message msg) {
> >            SnakeView.this.update();
> >            SnakeView.this.invalidate();
> >        }
>
> >        public void sleep(long delayMillis) {
> >                this.removeMessages(0);
> >            sendMessageDelayed(obtainMessage(0), delayMillis);
> >        }
> >    };
>
> > now, the ONLY place I can find in the code that actually references
> > mRedrawHandler is in the update function :
>
> > mRedrawHandler.sleep(mMoveDelay);
>
> > So, if I'm reading this correctly, the sleep method of RefreshHandler
> > gets called and it sends an empty message to itself after
> > [delayMillis] milliseconds.  When it receives that message, via
> > messageHandler, it repeats the update process etc.  To me this seems
> > pretty clever.  But why use a Handler?  By using a handler does it
> > utilize CPU cycles more efficiently for other processes when delaying
> > via the sendMessageDelayed function?  Counldn't you just do a
> > Thread.sleep() or something.  It seems the answer is "no" but I don't
> > understand why so I believe I'm missing something crucial.   I've
> > tried changing the RefreshHandler like so and the Snake program will
> > start but the game won't actually play.
>
> > private RefreshHandler mRedrawHandler = new RefreshHandler();
>
> >    class RefreshHandler {
>
> >        public void sleep(long delayMillis) {
> >                try{
> >                        Thread.sleep(delayMillis);
> >                }catch(Exception e){
> >                        Log.e(TAG, "Whatever");
> >                }
>
> >                SnakeView.this.update();
> >            SnakeView.this.invalidate();
>
> >        }
> >    };
>
> > It just kind loads up and after a few seconds goes to a Game Over
> > screen
>
> > If anyone could provide some remedial insight or direct me to a good
> > article it would be greatly appreciated.  I have a lot to learn but I
> > just got hung up on this particular concept.
>
> > Thanks.
>
> --
> Romain Guy
> Android framework engineer
> romain...@android.com
>
> 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 Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to