Skip to content Skip to sidebar Skip to footer

Android - Httpclient As A Backgroundservice

i have an app that login to a webservice and also uploads file. I need to keep the session alive as i go to different screens and get data from webservice. I read i need to make th

Solution 1:

Since a service runs on the same thread as the UI thread, you will need to run the service in a different thread. You can do this in several different ways:

  1. Use regular java threading within the service's onCreate () or onBind() etc, methods
  2. Use AsyncTask within the onCreate() method - another form of threading but much cleaner if you need to do UI updates
  3. Use IntentService which provides asynchronous Service task execution - not sure how well this works as I have never used it.

All three of these methods should allow you to make connections with the HttpClient in the background and through a Service and even though I have never used IntentService, it looks like the best option to me. AsyncTask would be very useful if you need to make changes to the UI which can only be done on the UI thread.

Edit by request: So I am currently doing something which requires Http connections in a asynchronous way. After making this post, I tried doing number 3 and it does work very well/easily. The only problem is that information has to be passed between two contexts through intents which is really ugly. So here is an approximate example of something you can do to make http connections in an asynchronous, background, service.

Launch the asynchronous service from an outside activity. I put two buttons just so the activity can be seen executing while the service is running. The intent can be launched really anywhere you feel like.

/* Can be executed when button is clicked, activity is launched, etc.
   Here I launch it from a OnClickListener of a button. Not really relevant to our interests.                       */publicvoidonClick(View v) {
        Intenti=newIntent ("com.test.services.BackgroundConnectionService");
        v.getContext().startService(i);         
    }

Then within the BackgroundConnectionService you have to extend the IntentService class and implement all http calls within the onHandleIntent(Intent intent) method. It is as easy as this example:

publicclassBackgroundConnectionServiceextendsIntentService {

    publicBackgroundConnectionService() {
        // Need this to name the servicesuper ("ConnectionServices");
    }

    @OverrideprotectedvoidonHandleIntent(Intent arg0) {
        // Do stuff that you want to happen asynchronously hereDefaultHttpClienthttpclient=newDefaultHttpClient ();
        HttpGethttpget=newHttpGet ("http://www.google.com");
        // Some try and catch that I am leaving out
        httpclient.execute (httpget);
    }
}

Finally, declare the asynchronous service as you would any normal service in the AndroidManifest.xml file within the <application> tags.

...
        <serviceandroid:name="com.test.services.BackgroundConnectionService"><intent-filter><actionandroid:name="com.test.services.BackgroundConnectionService" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter></service>
...

That should about do it. It is actually pretty easy : D

Post a Comment for "Android - Httpclient As A Backgroundservice"