package com.example.myfirstapp.service; import org.apache.http.client.HttpClient; import com.example.myfirstapp.io.PersonHandler; import com.example.myfirstapp.io.RemoteExecutor; import com.example.myfirstapp.utils.Utils; import android.app.IntentService; import android.content.ContentResolver; import android.content.Intent; import android.os.Bundle; import android.os.ResultReceiver; import android.util.Log; public class PersonService extends IntentService { private static final String TAG = "PersonsService"; public static final String EXTRA_STATUS = "com.example.myfirstapp.extra.STATUS"; public static final String EXTRA_REST_OPERATION = "com.example.myfirstapp.extra.REST_OPERATION"; public static final String GENERAL_REST_OPERATION = "com.example.myfirstapp.general.REST_OPERATION"; public static final int STATUS_RUNNING = 0x1; public static final int STATUS_ERROR = 0x2; public static final int STATUS_FINISHED = 0x3; private HttpClient mHttpClient; private ContentResolver mResolver; private RemoteExecutor mExecutor; @Override public void onCreate () { super.onCreate (); this.mHttpClient = Utils.getHttpClient ( this ); this.mResolver = getContentResolver (); this.mExecutor = new RemoteExecutor ( this.mHttpClient, this.mResolver ); } public PersonService () { super ( TAG ); } @Override protected void onHandleIntent ( Intent intent ) { Log.d ( TAG, "onHandleIntent()" ); final String restOperation = intent.getStringExtra ( EXTRA_REST_OPERATION ); if ( GENERAL_REST_OPERATION.equals ( restOperation ) ) getPersons ( intent ); } private void getPersons ( Intent intent ) { final ResultReceiver receiver = intent.getParcelableExtra ( EXTRA_STATUS ); if ( receiver != null ) receiver.send ( STATUS_RUNNING, Bundle.EMPTY ); try { mExecutor.executeGet ( Utils.buildProxyPersonsUri (), new PersonHandler () ); } catch ( Exception e ) { if ( receiver != null ) { final Bundle bundle = new Bundle (); bundle.putString ( Intent.EXTRA_TEXT, e.toString () ); receiver.send ( STATUS_ERROR, bundle ); } } if ( receiver != null ) receiver.send ( STATUS_FINISHED, Bundle.EMPTY ); } }