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

Reply via email to