Advertisement
vergepuppeter

LocationService

Nov 17th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1. package app.LocationService.Service;
  2.  
  3. /**
  4.  * Created by AhmadYusri on 11/11/2015.
  5.  */
  6. import android.app.Service;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.location.Location;
  11. import android.os.Binder;
  12. import android.os.Build;
  13. import android.os.Bundle;
  14. import android.os.IBinder;
  15. import android.os.Messenger;
  16. import android.support.v4.content.LocalBroadcastManager;
  17. import android.util.Log;
  18.  
  19. import com.google.android.gms.common.ConnectionResult;
  20. import com.google.android.gms.common.GooglePlayServicesUtil;
  21. import com.google.android.gms.common.api.GoogleApiClient;
  22. import com.google.android.gms.location.LocationListener;
  23. import com.google.android.gms.location.LocationRequest;
  24. import com.google.android.gms.location.LocationServices;
  25.  
  26. import java.text.SimpleDateFormat;
  27. import java.util.Date;
  28.  
  29. import app.fikri.com.labaikallahsystem.Constants;
  30.  
  31. public class LocationService extends Service implements
  32.         GoogleApiClient.ConnectionCallbacks,
  33.         GoogleApiClient.OnConnectionFailedListener,
  34.         LocationListener {
  35.  
  36.     private static final String TAG = "LocationService";
  37.     public final static String ACTION = "LOCATION_ACTION";
  38.     public final static String LATITUDE = "LOCATION_LATITUDE";
  39.     public final static String LONGITUDE = "LOCATION_LONGITUDE";
  40.  
  41.  
  42.     // use the websmithing defaultUploadWebsite for testing and then check your
  43.     // location with your browser here: https://www.websmithing.com/gpstracker/displaymap.php
  44.     private String defaultUploadWebsite;
  45.  
  46.     private boolean currentlyProcessingLocation = false;
  47.     private LocationRequest locationRequest;
  48.     private GoogleApiClient locationClient;
  49.  
  50.     @Override
  51.     public void onCreate() {
  52.         super.onCreate();
  53.         //defaultUploadWebsite = getString(R.string.default_upload_website);
  54.     }
  55.  
  56.     @Override
  57.     public int onStartCommand(Intent intent, int flags, int startId) {
  58.         // if we are currently trying to get a location and the alarm manager has called this again,
  59.         // no need to start processing a new location.
  60.         if (!currentlyProcessingLocation) {
  61.             currentlyProcessingLocation = true;
  62.             startTracking();
  63.         }
  64.  
  65.         return START_NOT_STICKY;
  66.     }
  67.  
  68.     private void startTracking() {
  69.         Log.d(TAG, "startTracking");
  70.  
  71.         if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
  72.             locationClient = new GoogleApiClient.Builder(this)
  73.                     .addApi(LocationServices.API)
  74.                     .addConnectionCallbacks(this)
  75.                     .addOnConnectionFailedListener(this).build();
  76.  
  77.  
  78.             if (!locationClient.isConnected() || !locationClient.isConnecting()) {
  79.                 locationClient.connect();
  80.             }
  81.         } else {
  82.             Log.e(TAG, "unable to connect to google play services.");
  83.         }
  84.     }
  85.  
  86.     protected void sendLocationDataToWebsite(Location location) {
  87.         // formatted for mysql datetime format
  88.         Log.i(TAG, String.valueOf(location.getLatitude()) + " " + String.valueOf(location.getLongitude()));
  89.  
  90.  
  91.     }
  92.  
  93.     @Override
  94.     public void onDestroy() {
  95.         super.onDestroy();
  96.  
  97.  
  98.         stopLocationUpdates();
  99.         stopSelf();
  100.  
  101.     }
  102.  
  103.     @Override
  104.     public IBinder onBind(Intent intent) {
  105.         return null;
  106.     }
  107.  
  108.     @Override
  109.     public void onLocationChanged(Location location) {
  110.         if (location != null) {
  111.             Log.e(TAG, "position: " + location.getLatitude() + ", " + location.getLongitude() + " accuracy: " + location.getAccuracy());
  112.  
  113.             // we have our desired accuracy of 500 meters so lets quit this service,
  114.             // onDestroy will be called and stop our location uodates
  115.             broadcastLocation(location.getLatitude(), location.getLongitude());
  116.             //sendLocationDataToWebsite(location);
  117.         }
  118.     }
  119.  
  120.     private void stopLocationUpdates() {
  121.         if (locationClient != null && locationClient.isConnected()) {
  122.             LocationServices.FusedLocationApi.removeLocationUpdates(locationClient, this);
  123.             locationClient.disconnect();
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * Called by Location Services when the request to connect the
  129.      * client finishes successfully. At this point, you can
  130.      * request the current location or start periodic updates
  131.      */
  132.     @Override
  133.     public void onConnected(Bundle bundle) {
  134.         Log.d(TAG, "onConnected");
  135.  
  136.         locationRequest = new LocationRequest();
  137.         locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  138.         locationRequest.setInterval(Constants.UPDATE_INTERVAL);
  139.         locationRequest.setFastestInterval(Constants.FASTEST_INTERVAL);
  140.         LocationServices.FusedLocationApi.requestLocationUpdates(locationClient,
  141.                 locationRequest, this);
  142.     }
  143.  
  144.     @Override
  145.     public void onConnectionSuspended(int i) {
  146.         stopLocationUpdates();
  147.     }
  148.  
  149.     /**
  150.      * Called by Location Services if the connection to the
  151.      * location client drops because of an error.
  152.      */
  153.  
  154.     @Override
  155.     public void onConnectionFailed(ConnectionResult connectionResult) {
  156.         Log.e(TAG, "onConnectionFailed");
  157.  
  158.         stopLocationUpdates();
  159.         stopSelf();
  160.     }
  161.  
  162.     public String getCurrentTime()
  163.     {
  164.         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
  165.         String currentTime = sdf.format(new Date());
  166.         return currentTime;
  167.     }
  168.  
  169.     public String getDeviceName() {
  170.         String manufacturer = Build.MANUFACTURER;
  171.         String model = Build.MODEL;
  172.         if (model.startsWith(manufacturer)) {
  173.             return capitalize(model);
  174.         } else {
  175.             return capitalize(manufacturer) + " " + model;
  176.         }
  177.     }
  178.  
  179.     private String capitalize(String s) {
  180.         if (s == null || s.length() == 0) {
  181.             return "";
  182.         }
  183.         char first = s.charAt(0);
  184.         if (Character.isUpperCase(first)) {
  185.             return s;
  186.         } else {
  187.             return Character.toUpperCase(first) + s.substring(1);
  188.         }
  189.     }
  190.  
  191.     private void broadcastLocation(double lat, double lng)
  192.     {
  193.         Intent intent = new Intent();
  194.         intent.setAction(ACTION);
  195.         intent.putExtra(LATITUDE, lat);
  196.         intent.putExtra(LONGITUDE, lng);
  197.         sendBroadcast(intent);
  198.     }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement