Thank you Dan.

I understand your post and appreciate your help.

I'll try it! :)

Sorry to bother, but i've never done concurrency.

On 15 ene, 17:51, Dan <danscott0...@gmail.com> wrote:
> Misato,
>
> I have included some code that I use and works great. I'm fairly new
> to Android myself so there obviously could be other ways of doing it
> as well.
>
> Inside of my activity class I create the Handler object:
>
>         // Create a new handler object that will handle the messages
>         // sent to the UI activity from the background thread.
>         Handler handler = new Handler()
>         {
>                 @Override public void handleMessage(Message msg)
>                 {
>                     // Handle processing of message from background
> thread.
>                 }
>         };
>
> Then in my onStart() is where I create my background thread. I would
> imagine you could create it in your onCreate() as well but I had seen
> examples and they all seemed to create it in the onStart(). I stripped
> out most of my code for simplification but left a few things in to
> show you how to obtain a message object and pass it to your UI thread.
>
>         @Override protected void onStart()
>         {
>                 super.onStart();
>
>                 mBackground = new Thread(new Runnable()
>                 {
>                         // Setup the run() method that is called when the 
> background thread
>                         // is started.
>                         public void run()
>                         {
>                                 // Do you background thread process
> here...
>                                 // In my case, strMsgRcvd is a String
> where I store
>                                 // the message received via my socket.
>
>                                 // Get a message object to be sent to
> our handler.
>                                 Message myMsg = handler.obtainMessage();
>
>                                 // Set the data into our handler message.
>                                 myMsg.obj = strMsgRcvd;
>
>                                 // Send the handler message to the UI thread.
>                                 handler.sendMessage(myMsg);
>
>                         }
>                 });
>
>                 // Make sure to start the background thread before leaving the
> onStart() method.
>                 mBackground.start();
>         }
>
> That should at least get you started. There are one or two things to
> remember when setting things up. Again for sake of simplicity, I have
> not included the code but will just mention them here. If you keep
> your background thread running continuously, (I do this by
> continuously looping and blocking on my "receive" call on the socket I
> created) you need to remember to clean things up in the onStop() or
> onResume() methods. This way if your app is interrupted, you won't
> have any problems when you come back in because your onStart() will
> get called again and things will be setup again.
>
> Anyway, I hope this helps...
> Dan
>
> On Jan 15, 4:48 am, "[^MiSaTo^]" <misato...@gmail.com> wrote:
>
> > So, sorry to bother you again.
> > What i understand, is that i create the UI as the main Activity Class
> > and inside it i lauch the other thread.
> > In pseudo-code this will be like :
>
> > onCreate{
> >     initialize the UI;
> >     launch socket thread();
> >    create Handler();
>
> > }
>
> > am i right?
>
> > On Jan 14, 8:17 pm, James Yum <j...@google.com> wrote:
>
> > > Hi Misato,
>
> > > Like Dan said, you most likely do not want to create a separate thread
> > > for the UI. Any UI updates should occur in the main thread (i.e. the
> > > thread where the Views were created). Please check this How To for
> > > handling expensive operations in the UI thread.
>
> > >http://code.google.com/android/kb/commontasks.html#threading
>
> > > Cheers,
> > > James
>
> > > On Wed, Jan 14, 2009 at 2:19 AM, [^MiSaTo^] <misato...@gmail.com> wrote:
>
> > > > Hi again,
>
> > > > I'm doing a little irc client, and now i've got 3 classes:
> > > > the "main" class launches 2 threads, one for the UI class (where i've
> > > > got a textview where to show everything) and another for the class
> > > > that contains the socket to connect to the irc server.
>
> > > > The problem is that my application crashes with a NullPointerException
> > > > and i think the problem comes when I launch the UI class. (I'm doing
> > > > it: Thread tUI = new Thread(ui.class);)
>
> > > > I don't know the best way to lauch this thread simultaneusly to the
> > > > socket one, could anybody help me?
>
> > > > Thanks in anticipation and sorry for my bad english
--~--~---------~--~----~------------~-------~--~----~
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