If your Service is running in the same process as your Activity, use a
static reference to your Activity.

E.g. MyActivity.ACTIVE_INSTANCE

Be sure to update the ACTIVE_INSTANCE in onCreate (set it to 'this')
and onDestroy (set it to 'null'). This will work when you can have
only one instance of you activity at the same time. If you can
multiple instances active, then this trick won't work.

If your Service may be running in a different process, you have to use
IDL and interprocess communication between the two.


On Jan 22, 6:31 pm, Flapjack <[email protected]> wrote:
> Frank, if my AsyncTask is located in my Service, how can I use it to
> update the UI in my Activity? I'm a bit confused by this- I thought
> the AsyncTask would have to be located inside the Activity class in
> order to update the UI within the Activity.
>
> On Jan 21, 8:20 pm, Frank Weiss <[email protected]> wrote:
>
>
>
> > Please reread the doc carefully. You can run stuff on the UI thread in three
> > of the methods. There's really no need to send a message if you make the
> > subclass of AsyncTask an inner class. In that case those three methods can
> > do anything on the enclosing Activity's behalf, like setting the contents of
> > a view or showing a dialog.
>
> > On Thu, Jan 21, 2010 at 3:22 PM, Flapjack <[email protected]> wrote:
> > > According to my research, which includes reputable sources (Mark
> > > Murphy et al), the most preferred way of polling a remote source and
> > > presenting said data to the user is by creating a service and using
> > > AsyncTask within that service to do the polling. I have done that.
>
> > > But, when I read the docs (http://developer.android.com/reference/
> > > android/os/AsyncTask.html), there seem to be several "Threading Rules"
> > > that conflict with this way of doing things: "The task instance must
> > > be created on the UI thread." and "execute(Params...) must be invoked
> > > on the UI thread." As stated, I have created the task instance on the
> > > Service thread (not the UI thread). Am I missing something?
>
> > > Also, when the AsyncTask finished, I sent out a Broadcast on
> > > onPostExecute, which is then picked up by the Activity, telling it to
> > > retrieve the final value again from the service (since I couldn't
> > > obviously update the UI from the service). I couldn't figure out any
> > > other way to return the result of the AsyncTask. Is this the correct
> > > practice?
>
> > > Java is not my native language so please bear with my ignorance.
> > > Thanks!
>
> > > --
> > > 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]<android-developers%2bunsubs­[email protected]>
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en
>
> > Please- Hide quoted text -
>
> - Show quoted text -

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