I want to use AsyncTask for update my db4o with a server. In the doInBackground method , I connect to the server, update the db4o, and schedule a pendingintents. Not modify UI or show any toast.
Initially, I had the following error:
Can't create handler inside thread that has not called Looper.prepare()
After adding the Looper.prepare(), works fine, but only for five updates (AsyncTask). I’ve read this topic: AsyncTask threads never die (Android) , and I don’t now that fails. When I throw the sixth update, the app crashes:
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
at android.os.Looper.prepare(Looper.java:74)
(...)

I’ve read in the documentation that I need the Looper.loop(), but whith this, the app crashes..
Example:
protected Integer doInBackground(Void... params) {
Looper.prepare();
update = new Update();
update.checknewObjects();
update.deleteOldObjects();
update.updateObjects();
Looper.loop();
}
Why do I need Looper?
Why the app crashes after five updates?
Where can I schedule Looper.loop()?
Thanks in advance!
If you want to use
AsyncTaskso use it, you shouldn’t mix it withLopper…already working on background thread and there is no reason to call Lopper, for what?
If your application need to do some kind of long computation so
AsyncTaskis very good tool for it because offers methods which working onUI Threadand allow to updateUIwith some progress of work because every User that will use your application should know that application “doing something” (when it takes more than 2-5 seconds).Also it’s generic and that provide some benefits.
If you do not need to update
UIjust use for exampleHandler.