Skip to content Skip to sidebar Skip to footer

Can't Call Handler In An Asynctask?

I want to create a background thread (execute even if my activity dies) that update my data location et send it to my server with an http resquest. But it occurs an exception: 02-0

Solution 1:

EDIT: I think I have to use a Service instead of an asyncTask.

Precisely. Creating a separate thread will not ensure that it is not killed when the application / activity exits, or when the operating system needs more resources. A Service doesn't ensure that either -- it may still be killed if the system is under really heavy load, but it's much less likely. Also (correct me if I'm wrong), Android will restart the Service once the system load softens.

Solution 2:

Just call (write it down in your code) Looper.prepare(), before you run your Thread. (It's a simple solution, but previously made me weird too.)

If you can't call the Handler which is in your Activity, then firstly, you should make your activity constructor, for example :

public SampleActivity() {
    super();
    // TODO Auto-generated constructor stub
}

then in your ASyncTask, initiate that Activity:

SampleActivitysampleAct=newSampleActivity();

and the final kick is call your Handler.

sampleAct.handler.sendMessage(msg);

I hope this clearly explains it to you.

Post a Comment for "Can't Call Handler In An Asynctask?"