Friday, June 11, 2010

Re: [android-developers] Re: how to display a Toast from an IntentService?

May be this will work:

MyService extends IntentService
{
Handler mMainThreadHandler = null;
onCreate() {
super.onCreate();
//initialize and populate the mMainThreadHandler
//because this method runs on the main thread
//unlike the deriver handle...method
}
onHandleIntent() {
//this runs on its own thread
//do your work
//post a message to mMainThreadHandler
}
....
//in that handle of the mMainThreadHandler
{
show the toast as this runs on the main thread now
}
}

I am a bit guessing here.

Satya

On Fri, Jun 11, 2010 at 1:46 PM, Dianne Hackborn <hackbod@android.com> wrote:
> Oh for doing it from the callback from IntentService, you need to do it from
> a thread that is actually running a responsive looper (which by definition
> IntentService does not, since the thread it is calling you on is there to
> run long-running operations).
> So schedule a message in a Handler of the main thread, and show it there.
>  Basically the same as any time you want to touch other parts of your UI.
>
> On Fri, Jun 11, 2010 at 10:42 AM, Flying Coder <av8r.steve@gmail.com> wrote:
>>
>> Hi Mark,
>>    Thanks for the suggestion, but unfortunately, SystemClock.sleep()
>> doesn't do the trick. :-(
>>
>> Cheers,
>> Steve
>>
>>
>> On Jun 11, 1:22 pm, Mark Murphy <mmur...@commonsware.com> wrote:
>> > Flying Coder wrote:
>> > > So, I have an IntentService that handles button callbacks from a
>> > > widget.  When the user presses a certain button, I want to display a
>> > > Toast.  I return from onHandleIntent almost immediately after doing
>> > > Toast.show(), which in turn stops the service and kills its thread,
>> > > which keeps the Toast from actually being displayed.
>> >
>> > That's interesting. I've never tried that pattern. I'm a bit surprised
>> > that the Toast doesn't fire.
>> >
>> > > Can anyone suggest an easy way to get the Toast (or something similar)
>> > > to display from an IntentService?
>> >
>> > Have you tried SystemClock.sleep()? It's a serious kludge, but I don't
>> > know what else you can do.
>> >
>> > --
>> > Mark Murphy (a Commons
>> > Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>> >
>> > Android Consulting:http://commonsware.com/consulting
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to android-developers@googlegroups.com
>> To unsubscribe from this group, send email to
>> android-developers+unsubscribe@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>
>
>
> --
> Dianne Hackborn
> Android framework engineer
> hackbod@android.com
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  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 Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment