Monday, September 13, 2010

[android-developers] Re: AsyncTask doesn't run sometimes despite state returning RUNNING?

In general, one should either use Looper and Handler, or use
AsyncTask. Mixing and matching the two is a recipe for confusion. If
AsyncTask offers you the flexibility and control you need, use it and
forget about Looper/Handler.

The rules you cite are only for AsyncTask and its handy methods. They
do not apply to Looper/Handler which, being more flexible, have a more
complicated/subtle set of rules. I haven't seen them documented in one
place like for AsyncTask though.

One of my favorite books on Android refers to Looper/Handler as "the
Swiss-army knife" of concurrency on Android. But at least a Swiss-army
knife doesn't need a detailed set of Javadoc documentation to figure
out how to use it;)

On Sep 13, 8:11 am, Kostya Vasilyev <kmans...@gmail.com> wrote:
>   Donal,
>
> You having to call Looper.prepare makes it seem like you're not creating
> your task and calling its execute() from the UI thread.
>
> Is that so? The docs is actually quite clear on this, see "Threading rules":
>
> http://developer.android.com/reference/android/os/AsyncTask.html
>
> 13.09.2010 18:50, Donal Rafferty пишет:
>
>
>
> > Kostya,
>
> > I do create a new Task as follows everytime:
>
> > new SelectRingtoneTask().execute();
>
> > But I had to use Looper.prepare(); to get it to work in my Service,
> > this could be causing an issue?
>
> > Is there a proper way to use a AsyncTask in a Service without needing
> > to call Looper.prepare();?
>
> > 2010/9/13 Kostya Vasilyev <kmans...@gmail.com <mailto:kmans...@gmail.com>>
>
> >     Donal,
>
> >     It sort of seems like you reuse the same task over and over.
>
> >     What happens if you create a new task each time you need to
> >     perform the asynchronous operation?
>
> >     -- Kostya
>
> >     13.09.2010 17:44, Donal Rafferty пишет:
> >>     Kostya,
>
> >>     Thanks, that conversation can be seen here:
>
> >>    http://groups.google.com/group/android-developers/browse_thread/threa...
>
> >>     However the condition is in the cancel code, which I no longer
> >>     use as I am running the code in a Service so I have no need to
> >>     cancel it.
>
> >>     2010/9/13 Kostya Vasilyev <kmans...@gmail.com
> >>     <mailto:kmans...@gmail.com>>
>
> >>         Donal,
>
> >>         Search the list: there was a post within the last month (I
> >>         think) from Romain Guy about some kind of thread race issue
> >>         in AsyncTask having been fixed in Froyo (or was it
> >>         post-Froyo?) - anyway, it's somewhere in the archives.
>
> >>         -- Kostya
>
> >>         13.09.2010 16:07, Donal Rafferty пишет:
> >>>         Sorry I meant only the preExecute is run
>
> >>>         On Mon, Sep 13, 2010 at 1:05 PM, Donal Rafferty
> >>>         <draf...@gmail.com <mailto:draf...@gmail.com>> wrote:
>
> >>>             I have managed to implement the Task in my Service now
> >>>             but the original problem still remains.
>
> >>>             I dont cancel the Task at all in my service, I simply
> >>>             play the ringtone in the postExecute method of the
> >>>             Asynctask if a flag is true, if its not I dont.
>
> >>>             But I get the same issue, after a couple of runs the
> >>>             AsyncTask simply stops getting to the doInBackground
> >>>             method, only the postExecute method is run.
>
> >>>             Its very strange and really annoying me now.
>
> >>>             Is there any reason why an AsyncTask would behave like
> >>>             this without cancel being used?
>
> >>>             On Mon, Sep 13, 2010 at 10:58 AM, Donal Rafferty
> >>>             <draf...@gmail.com <mailto:draf...@gmail.com>> wrote:
>
> >>>                 Monday morning coding! :)
>
> >>>                 I return my aidl definition in my service's onBind:
>
> >>>                 @Override
> >>>                     public IBinder onBind(Intent arg0) {
> >>>                         Log.d("XXX", "Status: onBind Called");
> >>>                         return myServiceStub;
> >>>                     }
>
> >>>                     IBinder myServiceStub = new IMyService.Stub(){
>
> >>>                      //aidl methods
>
> >>>                     };
>
> >>>                 And use this to call methods from my Activity, so I
> >>>                 would need to be able to import
> >>>                 android.media.Ringtone in my aidl definition but I cant.
>
> >>>                 So I'm still stuck with it not working :(
>
> >>>                 On Mon, Sep 13, 2010 at 10:32 AM, Kostya Vasilyev
> >>>                 <kmans...@gmail.com <mailto:kmans...@gmail.com>> wrote:
>
> >>>                     Donal,
>
> >>>                     No, a service is a service, and a binder is a
> >>>                     binder.
>
> >>>                     The binder you get in the callback is the binder
> >>>                     returned by the service's onBind.
>
> >>>                     This example:
>
> >>>                    http://developer.android.com/resources/samples/ApiDemos/src/com/examp...
>
> >>>                     has a binder implementation that returns a
> >>>                     reference to its service, so you can do this
>
> >>>                     public void onServiceConnected(ComponentName
> >>>                     className,
> >>>                                  IBinder binder) {
>
> >>>                     LocalService.LocalBinder binder =
> >>>                     (LocalService.LocalBinder) binder;
> >>>                     *LocalService service = binder.getService();*
> >>>                     }
>
> >>>                     This example has one flaw: it leaks a service
> >>>                     reference (since LocalBinder is a non-static
> >>>                     inner class of LocalService). Using a static
> >>>                     inner or a top-level class for LocalBinder,
> >>>                     fixes that (AFAIK).
>
> >>>                     -- Kostya
>
> >>>                     13.09.2010 13:16, Donal Rafferty пишет:
> >>>>                     09-13 10:06:53.566:
> >>>>                     ERROR/AndroidRuntime(18791): Uncaught handler:
> >>>>                     thread main exiting due to uncaught exception
> >>>>                     09-13 10:06:53.676:
> >>>>                     ERROR/AndroidRuntime(18791):
> >>>>                     java.lang.ClassCastException:
> >>>>                     com.xxx.phone.MyService$1
> >>>>                     09-13 10:06:53.676:
> >>>>                     ERROR/AndroidRuntime(18791):     at
> >>>>                     com.xxx.phone.ui.MyActivity$2.onServiceConnected(MyActivity.java:515)
>
> >>>>                     So it doesn't like the cast from binder to Service?
>
> >>>>                     How can I fix that do you know? What am I
> >>>>                     missing from the example?
>
> >>>                     --
> >>>                     Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com
>
> >>>                     --
> >>>                     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
> >>>                     <mailto:android-developers@googlegroups.com>
> >>>                     To unsubscribe from this group, send email to
> >>>                     android-developers+unsubscribe@googlegroups.com
> >>>                     <mailto:android-developers%2Bunsubscribe@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
> >>>         <mailto:android-developers@googlegroups.com>
> >>>         To unsubscribe from this group, send email to
> >>>         android-developers+unsubscribe@googlegroups.com
> >>>         <mailto:android-developers+unsubscribe@googlegroups.com>
> >>>         For more options, visit this group at
> >>>        http://groups.google.com/group/android-developers?hl=en
>
> >>         --
> >>         Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com
>
> >>         --
> >>         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
> >>         <mailto:android-developers@googlegroups.com>
> >>         To unsubscribe from this group, send email to
> >>         android-developers+unsubscribe@googlegroups.com
> >>         <mailto:android-developers%2Bunsubscribe@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
> >>     <mailto:android-developers@googlegroups.com>
> >>     To unsubscribe from this group, send email to
> >>     android-developers+unsubscribe@googlegroups.com
> >>     <mailto:android-developers+unsubscribe@googlegroups.com>
> >>     For more options, visit this group at
> >>    http://groups.google.com/group/android-developers?hl=en
>
> >     --
> >     Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com
>
> >     --
> >     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
> >     <mailto:android-developers@googlegroups.com>
> >     To unsubscribe from this group, send email to
> >     android-developers+unsubscribe@googlegroups.com
> >     <mailto:android-developers%2Bunsubscribe@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
>
> --
> Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com

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