title: Making a Service out of Basic Location Sample code
body: <p>I want to make a service (background operation) in Android to track the location of a device every now and then and store it on SharedPreference or send it to my server when location changes. I came accross this code from <a href="https://stackoverflow.com/questions/29457634/get-current-location-name-of-user-without-using-gps-or-internet-but-by-using-net?lq=1" title="stackoverflow">Get current location name of user without using gps or internet but by using Network_Provider in android</a> and <a href="http://developer.android.com/training/location/retrieve-current.html#last-known" rel="nofollow noreferrer" title="Android Developers">Android Developers - Getting the Last Known Location</a>.</p>
  
  <p>This is the code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements
  ConnectionCallbacks, OnConnectionFailedListener {
  
      protected static final String TAG = "MainActivity";
  
      /**
      * Provides the entry point to Google Play services.
      */
      protected GoogleApiClient mGoogleApiClient;
  
      /**
      * Represents a geographical location.
      */
      protected Location mLastLocation;
  
      protected String mLatitudeLabel;
      protected String mLongitudeLabel;
      protected TextView mLatitudeText;
      protected TextView mLongitudeText;
  
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main_activity);
  
          mLatitudeLabel = getResources().getString(R.string.latitude_label);
          mLongitudeLabel = getResources().getString(R.string.longitude_label);
          mLatitudeText = (TextView) findViewById((R.id.latitude_text));
          mLongitudeText = (TextView) findViewById((R.id.longitude_text));
  
          buildGoogleApiClient();
      }
  
      /**
      * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
      */
      protected synchronized void buildGoogleApiClient() {
          mGoogleApiClient = new GoogleApiClient.Builder(this)
          .addConnectionCallbacks(this)
          .addOnConnectionFailedListener(this)
          .addApi(LocationServices.API)
          .build();
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          mGoogleApiClient.connect();
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          if (mGoogleApiClient.isConnected()) {
              mGoogleApiClient.disconnect();
          }
      }
  
      /**
      * Runs when a GoogleApiClient object successfully connects.
      */
      @Override
      public void onConnected(Bundle connectionHint) {
          // Provides a simple way of getting a device's location and is well suited for
          // applications that do not require a fine-grained location and that do not need location
          // updates. Gets the best and most recent location currently available, which may be null
          // in rare cases when a location is not available.
          mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
          if (mLastLocation != null) {
              Log.e("Location", "not null");
              makeUseOfNewLocation(mLastLocation);
              } else {
              Toast.makeText(this, R.string.no_location_detected, Toast.LENGTH_LONG).show();
              // should request new one
              // location should be enabled
              Log.i(TAG,
              "No location data previously acquired.. should request!");
  
              Toast.makeText(this,
              "Requesting location data ..",
              Toast.LENGTH_SHORT).show();
  
              LocationRequest locationRequest = LocationRequest.create();
              locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
              locationRequest.setInterval(5000);
  
              PendingResult&lt;Status&gt; result = LocationServices.FusedLocationApi
              .requestLocationUpdates(mGoogleApiClient,
              locationRequest,
              new LocationListener() {
  
                  @Override
                  public void onLocationChanged(Location location) {
                      Log.e("Location", "not null");
                      makeUseOfNewLocation(location);
                  }
              });
              // TODO: use result to retrieve more info
              Toast.makeText(this, result.toString(),Toast.LENGTH_LONG);
          }
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult result) {
          // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
          // onConnectionFailed.
          Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
      }
  
  
      @Override
      public void onConnectionSuspended(int cause) {
          // The connection to Google Play services was lost for some reason. We call connect() to
          // attempt to re-establish the connection.
          Log.i(TAG, "Connection suspended");
          mGoogleApiClient.connect();
      }
      private void makeUseOfNewLocation(Location location) {
          // do your stuff here
          mLatitudeText.setText(String.format("%s: %f", mLatitudeLabel,
          location.getLatitude()));
          mLongitudeText.setText(String.format("%s: %f", mLongitudeLabel,
          location.getLongitude()));
      }
  }
  </code></pre>
  
  <p>My question is can this code be a Service by just extending Service on this class. Will this work and be able to track the device's location every now and then on the background</p>