Guest User

Untitled

a guest
Apr 24th, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. package net.touchsf.testnewgoogleplayservices;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.location.Location;
  6. import android.os.Bundle;
  7. import android.os.IBinder;
  8. import android.util.Log;
  9. import android.widget.Toast;
  10.  
  11. import com.google.android.gms.common.ConnectionResult;
  12. import com.google.android.gms.common.api.GoogleApiClient;
  13. import com.google.android.gms.location.LocationListener;
  14. import com.google.android.gms.location.LocationRequest;
  15. import com.google.android.gms.location.LocationServices;
  16.  
  17. public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
  18.         GoogleApiClient.OnConnectionFailedListener, LocationListener {
  19.  
  20.     // LogCat tag
  21.     private static final String TAG = LocationService.class.getSimpleName();
  22.  
  23.     private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
  24.  
  25.     private Location mLastLocation;
  26.  
  27.     // Google client to interact with Google API
  28.     private GoogleApiClient mGoogleApiClient;
  29.  
  30.     // boolean flag to toggle periodic location updates
  31.     private boolean mRequestingLocationUpdates = false;
  32.  
  33.     private LocationRequest mLocationRequest;
  34.  
  35.     // Location updates intervals in sec
  36.     private static int UPDATE_INTERVAL = 10000; // 10 sec
  37.     private static int FATEST_INTERVAL = 5000; // 5 sec
  38.     private static int DISPLACEMENT = 40; // 10 meters
  39.  
  40.  
  41.     @Override
  42.     public void onCreate() {
  43.         super.onCreate();
  44.  
  45.         mGoogleApiClient = new GoogleApiClient.Builder(this)
  46.                 .addConnectionCallbacks(this)
  47.                 .addOnConnectionFailedListener(this)
  48.                 .addApi(LocationServices.API).build();
  49.  
  50.     }
  51.  
  52.     @Override
  53.     public int onStartCommand(Intent intent, int flags, int startId) {
  54.         if (mGoogleApiClient != null) {
  55.             mGoogleApiClient.connect();
  56.             togglePeriodicLocationUpdates();
  57.         }
  58.         return START_NOT_STICKY;
  59.     }
  60.  
  61.     @Override
  62.     public IBinder onBind(Intent intent) {
  63.         return null;
  64.     }
  65.  
  66.     protected void createLocationRequest() {
  67.         mLocationRequest = new LocationRequest();
  68.         mLocationRequest.setInterval(UPDATE_INTERVAL);
  69.         mLocationRequest.setFastestInterval(FATEST_INTERVAL);
  70.         mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  71.         mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
  72.     }
  73.  
  74.     private void togglePeriodicLocationUpdates() {
  75.         mGoogleApiClient.connect();
  76.  
  77.         if (!mRequestingLocationUpdates) {
  78.  
  79.             mRequestingLocationUpdates = true;
  80.  
  81.             startLocationUpdates();
  82.  
  83.             Log.d(TAG, "Periodic location updates started!");
  84.  
  85.         } else {
  86.  
  87.             mRequestingLocationUpdates = false;
  88.  
  89.             // Stopping the location updates
  90.             stopLocationUpdates();
  91.  
  92.             Log.d(TAG, "Periodic location updates stopped!");
  93.         }
  94.     }
  95.  
  96.     protected void stopLocationUpdates() {
  97.         LocationServices.FusedLocationApi.removeLocationUpdates(
  98.                 mGoogleApiClient, this);
  99.     }
  100.  
  101.     protected void startLocationUpdates() {
  102.         mGoogleApiClient.connect();
  103.         LocationServices.FusedLocationApi.requestLocationUpdates(
  104.                 mGoogleApiClient, mLocationRequest, this);
  105.  
  106.     }
  107.  
  108.     @Override
  109.     public void onConnected(Bundle arg0) {
  110.         createLocationRequest();
  111.     }
  112.  
  113.     @Override
  114.     public void onConnectionSuspended(int arg0) {
  115.         mGoogleApiClient.connect();
  116.     }
  117.  
  118.     @Override
  119.     public void onConnectionFailed(ConnectionResult result) {
  120.         Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
  121.                 + result.getErrorCode());
  122.     }
  123.  
  124.     @Override
  125.     public void onLocationChanged(Location location) {
  126.         // Assign the new location
  127.         mLastLocation = location;
  128.  
  129.         Toast.makeText(getApplicationContext(), "Location changed!",
  130.                 Toast.LENGTH_SHORT).show();
  131.     }
  132.  
  133.     @Override
  134.     public boolean stopService(Intent name) {
  135.         return super.stopService(name);
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment