Created
January 28, 2019 05:18
-
-
Save fida1989/2b0b52614477c5ca281a3c5ea2f44cf5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyLocationService extends Service { | |
private static final String TAG = "MyLocationService"; | |
private LocationManager mLocationManager = null; | |
private static final int LOCATION_INTERVAL = 1000; | |
private static final float LOCATION_DISTANCE = 10f; | |
private class LocationListener implements android.location.LocationListener { | |
Location mLastLocation; | |
public LocationListener(String provider) { | |
Log.e(TAG, "LocationListener " + provider); | |
mLastLocation = new Location(provider); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.e(TAG, "onLocationChanged: " + location); | |
mLastLocation.set(location); | |
System.out.println(TAG+" "+location.getLatitude()+ " : "+location.getLongitude()); | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
Log.e(TAG, "onProviderDisabled: " + provider); | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
Log.e(TAG, "onProviderEnabled: " + provider); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
Log.e(TAG, "onStatusChanged: " + provider); | |
} | |
} | |
/* | |
LocationListener[] mLocationListeners = new LocationListener[]{ | |
new LocationListener(LocationManager.GPS_PROVIDER), | |
new LocationListener(LocationManager.NETWORK_PROVIDER) | |
}; | |
*/ | |
LocationListener[] mLocationListeners = new LocationListener[]{ | |
new LocationListener(LocationManager.GPS_PROVIDER) | |
}; | |
@Override | |
public IBinder onBind(Intent arg0) { | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Log.e(TAG, "onStartCommand"); | |
super.onStartCommand(intent, flags, startId); | |
return START_STICKY; | |
} | |
@Override | |
public void onCreate() { | |
Log.e(TAG, "onCreate"); | |
initializeLocationManager(); | |
try { | |
mLocationManager.requestLocationUpdates( | |
LocationManager.GPS_PROVIDER, | |
LOCATION_INTERVAL, | |
LOCATION_DISTANCE, | |
mLocationListeners[0] | |
); | |
} catch (SecurityException ex) { | |
Log.i(TAG, "fail to request location update, ignore", ex); | |
} catch (IllegalArgumentException ex) { | |
Log.d(TAG, "network provider does not exist, " + ex.getMessage()); | |
} | |
} | |
@Override | |
public void onDestroy() { | |
Log.e(TAG, "onDestroy"); | |
super.onDestroy(); | |
if (mLocationManager != null) { | |
for (int i = 0; i < mLocationListeners.length; i++) { | |
try { | |
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
return; | |
} | |
mLocationManager.removeUpdates(mLocationListeners[i]); | |
} catch (Exception ex) { | |
Log.i(TAG, "fail to remove location listener, ignore", ex); | |
} | |
} | |
} | |
} | |
private void initializeLocationManager() { | |
Log.e(TAG, "initializeLocationManager - LOCATION_INTERVAL: "+ LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE); | |
if (mLocationManager == null) { | |
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment