Advertisement
rachmadi

Location Service

Feb 28th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. import android.app.Service;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.location.Location;
  5. import android.location.LocationManager;
  6. import android.os.Bundle;
  7. import android.os.IBinder;
  8. import android.util.Log;
  9.  
  10. public class MyService extends Service
  11. {
  12.     private static final String TAG = "BOOMBOOMTESTGPS";
  13.     private LocationManager mLocationManager = null;
  14.     private static final int LOCATION_INTERVAL = 1000;
  15.     private static final float LOCATION_DISTANCE = 10f;
  16.  
  17.     private class LocationListener implements android.location.LocationListener
  18.     {
  19.         Location mLastLocation;
  20.  
  21.         public LocationListener(String provider)
  22.         {
  23.             Log.e(TAG, "LocationListener " + provider);
  24.             mLastLocation = new Location(provider);
  25.         }
  26.  
  27.         @Override
  28.         public void onLocationChanged(Location location)
  29.         {
  30.             Log.e(TAG, "onLocationChanged: " + location);
  31.             mLastLocation.set(location);
  32.         }
  33.  
  34.         @Override
  35.         public void onProviderDisabled(String provider)
  36.         {
  37.             Log.e(TAG, "onProviderDisabled: " + provider);
  38.         }
  39.  
  40.         @Override
  41.         public void onProviderEnabled(String provider)
  42.         {
  43.             Log.e(TAG, "onProviderEnabled: " + provider);
  44.         }
  45.  
  46.         @Override
  47.         public void onStatusChanged(String provider, int status, Bundle extras)
  48.         {
  49.             Log.e(TAG, "onStatusChanged: " + provider);
  50.         }
  51.     }
  52.  
  53.     LocationListener[] mLocationListeners = new LocationListener[] {
  54.             new LocationListener(LocationManager.GPS_PROVIDER),
  55.             new LocationListener(LocationManager.NETWORK_PROVIDER)
  56.     };
  57.  
  58.     @Override
  59.     public IBinder onBind(Intent arg0)
  60.     {
  61.         return null;
  62.     }
  63.  
  64.     @Override
  65.     public int onStartCommand(Intent intent, int flags, int startId)
  66.     {
  67.         Log.e(TAG, "onStartCommand");
  68.         super.onStartCommand(intent, flags, startId);
  69.         return START_STICKY;
  70.     }
  71.  
  72.     @Override
  73.     public void onCreate()
  74.     {
  75.         Log.e(TAG, "onCreate");
  76.         initializeLocationManager();
  77.         try {
  78.             mLocationManager.requestLocationUpdates(
  79.                     LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
  80.                     mLocationListeners[1]);
  81.         } catch (java.lang.SecurityException ex) {
  82.             Log.i(TAG, "fail to request location update, ignore", ex);
  83.         } catch (IllegalArgumentException ex) {
  84.             Log.d(TAG, "network provider does not exist, " + ex.getMessage());
  85.         }
  86.         try {
  87.             mLocationManager.requestLocationUpdates(
  88.                     LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
  89.                     mLocationListeners[0]);
  90.         } catch (java.lang.SecurityException ex) {
  91.             Log.i(TAG, "fail to request location update, ignore", ex);
  92.         } catch (IllegalArgumentException ex) {
  93.             Log.d(TAG, "gps provider does not exist " + ex.getMessage());
  94.         }
  95.     }
  96.  
  97.     @Override
  98.     public void onDestroy()
  99.     {
  100.         Log.e(TAG, "onDestroy");
  101.         super.onDestroy();
  102.         if (mLocationManager != null) {
  103.             for (int i = 0; i < mLocationListeners.length; i++) {
  104.                 try {
  105.                     mLocationManager.removeUpdates(mLocationListeners[i]);
  106.                 } catch (Exception ex) {
  107.                     Log.i(TAG, "fail to remove location listners, ignore", ex);
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     private void initializeLocationManager() {
  114.         Log.e(TAG, "initializeLocationManager");
  115.         if (mLocationManager == null) {
  116.             mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement