Johan
Johan

Reputation: 35223

AsyncTask in Service - overkill?

Im currently doing some network related stuff,

public void postData() {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.xx.xx.php");

try {

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
} 

in an asynctask, which i am executing from a service.

What i would like to know is if its enough to run the network methods directly in the service? Because AFAIK it doesnt run on the UI thread.

Thanks

Upvotes: 2

Views: 506

Answers (2)

user
user

Reputation: 87064

An IntentService has its own thread and you don't need the AsyncTask but a normal Service runs on the UI thread and you need to move the operations that take a long time to another thread(like using the AsyncTask). The Service running on the main UI thread is stated in the docs.

Upvotes: 3

Michał Klimczak
Michał Klimczak

Reputation: 13144

From documentation:

A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

So t's not an overkill unless you use IntentService

Upvotes: 2

Related Questions