Advertisement
MaryOrange

Service

Nov 22nd, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package com.example.myfirstapp.service;
  2.  
  3. import org.apache.http.client.HttpClient;
  4. import com.example.myfirstapp.io.PersonHandler;
  5. import com.example.myfirstapp.io.RemoteExecutor;
  6. import com.example.myfirstapp.utils.Utils;
  7. import android.app.IntentService;
  8. import android.content.ContentResolver;
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.os.ResultReceiver;
  12. import android.util.Log;
  13.  
  14. public class PersonService extends IntentService {
  15.  
  16.     private static final String TAG = "PersonsService";
  17.  
  18.     public static final String EXTRA_STATUS = "com.example.myfirstapp.extra.STATUS";
  19.     public static final String EXTRA_REST_OPERATION = "com.example.myfirstapp.extra.REST_OPERATION";
  20.     public static final String GENERAL_REST_OPERATION = "com.example.myfirstapp.general.REST_OPERATION";
  21.  
  22.     public static final int STATUS_RUNNING = 0x1;
  23.     public static final int STATUS_ERROR = 0x2;
  24.     public static final int STATUS_FINISHED = 0x3;
  25.  
  26.     private HttpClient mHttpClient;
  27.     private ContentResolver mResolver;
  28.     private RemoteExecutor mExecutor;
  29.  
  30.     @Override
  31.     public void onCreate () {
  32.         super.onCreate ();
  33.         this.mHttpClient = Utils.getHttpClient ( this );
  34.         this.mResolver = getContentResolver ();
  35.         this.mExecutor = new RemoteExecutor ( this.mHttpClient, this.mResolver );
  36.     }
  37.  
  38.     public PersonService () {
  39.         super ( TAG );
  40.     }
  41.  
  42.     @Override
  43.     protected void onHandleIntent ( Intent intent ) {
  44.         Log.d ( TAG, "onHandleIntent()" );
  45.         final String restOperation = intent.getStringExtra ( EXTRA_REST_OPERATION );
  46.         if ( GENERAL_REST_OPERATION.equals ( restOperation ) )
  47.             getPersons ( intent );
  48.     }
  49.  
  50.     private void getPersons ( Intent intent ) {
  51.         final ResultReceiver receiver = intent.getParcelableExtra ( EXTRA_STATUS );
  52.         if ( receiver != null )
  53.             receiver.send ( STATUS_RUNNING, Bundle.EMPTY );
  54.  
  55.         try {
  56.             mExecutor.executeGet ( Utils.buildProxyPersonsUri (), new PersonHandler () );
  57.         } catch ( Exception e ) {
  58.             if ( receiver != null ) {
  59.                 final Bundle bundle = new Bundle ();
  60.                 bundle.putString ( Intent.EXTRA_TEXT, e.toString () );
  61.                 receiver.send ( STATUS_ERROR, bundle );
  62.             }
  63.         }
  64.  
  65.         if ( receiver != null )
  66.             receiver.send ( STATUS_FINISHED, Bundle.EMPTY );
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement