It works well if it is the main thread. In all my version I uses this
way.

There isn't long-running work when I update my widget but sometime
(very very rarely)
I got ANR. For example when I try to get application label from
PackageManager.

I thought that I can avoid these ANRs with AsyncTask but it seems it
cause other problems.

Thank you for your answer, I won't use AsyncTask then.

On Apr 20, 6:20 pm, Dianne Hackborn <hack...@android.com> wrote:
> Do your work on the main thread, at least the stuff retrieving the bluetooth
> adapter.  Is there a reason you are using AsyncTask for this anyway?  Is
> there some long-running work you are also doing?
>
> 2011/4/20 b_t <bartata...@gmail.com>
>
>
>
>
>
>
>
>
>
> > I don't know what firmware it is but when I updated my app today I got
> > tons of error mails from different types of devices so it must be a
> > popular one.
>
> > On Apr 20, 4:17 pm, Kostya Vasilyev <kmans...@gmail.com> wrote:
> > > Have you looked at Android's own Power Controls widget? It's part of
> > > Settings application.
>
> > > Also, is it possible you're dealing with some heavily modified, buggy
> > > firmware?
> > >  20.04.2011 18:00 пользователь "b_t" <bartata...@gmail.com> написал:>
> > The original stack trace is:
>
> > > > Caused by: java.lang.RuntimeException: Can't create handler inside
> > > > thread that has not called Looper.prepare()
> > > > at android.os.Handler.<init>(Handler.java:121)
> > > > at android.bluetooth.BluetoothAdapter
> > > > $1.<init>(BluetoothAdapter.java:961)
> > > > at
> > > > android.bluetooth.BluetoothAdapter.<init>(BluetoothAdapter.java:961)
> > > > at
>
> > android.bluetooth.BluetoothAdapter.getDefaultAdapter(BluetoothAdapter.java:
>
> > > > 308)
> > > > *** myTask.doInBackground() ***
> > > > at android.os.AsyncTask$2.call(AsyncTask.java:185)
> > > > at java.util.concurrent.FutureTask
> > > > $Sync.innerRun(FutureTask.java:305)
> > > > at java.util.concurrent.FutureTask.run(FutureTask.java:137)
> > > > at
>
> > java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:
> > > > 1068)
> > > > at java.util.concurrent.ThreadPoolExecutor
> > > > $Worker.run(ThreadPoolExecutor.java:561)
> > > > at java.lang.Thread.run(Thread.java:1102)
>
> > > > I try to explain what I want:
>
> > > > - I have an appwidget with a bluetooth toggle
> > > > - If the bluetooth state changes the appwidget will be updated
> > > > - If I click on the appwidget than I toggle bluetooth state
>
> > > > If I click on the toggle:
>
> > > > - an IntentService will be executed
> > > > - it creates a Handler and displays a toast by Handler.post() with the
> > > > message "turning off..."
> > > > - I change bluetooth state
> > > > - bluetooth state changes so my broadcast receiver tries to update
> > > > appwidget
>
> > > > If I update appwidget in a service than everything is ok except that
> > > > APN can occur if the
> > > > update process is slow for some reason.
>
> > > > I would like to put appwidget update mechanism into an AsyncTask.
> > > > But because of BluetoothAdapter.getDefaultAdapter creates a Handler
> > > > (in some roms) it throws
> > > > the exception above.
>
> > > > If I put Looper.prepare() in the begin of doInBackground, then the
> > > > following error occurs
> > > > for a thread (there are five asynctask thread in my device so it
> > > > happens very early)
>
> > > > java.lang.RuntimeException: Only one Looper may be created per
> > > > thread
>
> > > > If I use the following code:
>
> > > > Looper myLooper = Looper.myLooper();
> > > > if (myLooper == null) {
> > > > Looper.prepare();
> > > > }
>
> > > > than everything seems fine except one thing: to toast I would like to
> > > > display
> > > > in the beginning of the process will be displayed much time later.
>
> > > > I don't know why and how can I avoid it.
>
> > > > I hope somebody understand my problem and can give me a solution.
>
> > > > T.
>
> > > > On Apr 20, 10:17 am, Kostya Vasilyev <kmans...@gmail.com> wrote:
> > > >> You could try calling Looper.prepare() first thing in
> > doInBackground().
>
> > > >> Oh, and logcat output would help us help you, as usual.
>
> > > >> -- Kostya
>
> > > >> 20.04.2011 12:07, b_t пишет:
>
> > > >> > Hi,
>
> > > >> > In my app I was going to use an AsyncTask to update appwidget data
> > > >> > because this process can be long. To get the state I have to call
> > > >> > BluetoothAdapter.getDefaultAdapter(). The problem is that in some
> > > >> > device (not in mine) it tries to initialize a Handler and throws
> > > >> > Exception about
> > > >> > missing Looper.
>
> > > >> > I don't know what should I do.
>
> > > >> --
> > > >> Kostya Vasilyev --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+unsubscr...@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+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to