Skip to content

Instantly share code, notes, and snippets.

@mkantar
Created April 30, 2017 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkantar/a3bb284fbbbe2c21052c5ec20dd64efe to your computer and use it in GitHub Desktop.
Save mkantar/a3bb284fbbbe2c21052c5ec20dd64efe to your computer and use it in GitHub Desktop.
Location provider service
public class LocationDataProvider {
private Context context;
private LocationManager locationManager;
private boolean isGPSProviderEnabled;
private boolean isNetworkProviderEnabled;
public static class HighAccuracyGPSNeeded extends Exception{
public HighAccuracyGPSNeeded() {
super("High Accuracy Mode is not opened!");
}
}
public LocationDataProvider(Context context){
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
public Location getLocation() throws HighAccuracyGPSNeeded,SecurityException{
isGPSProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isGPSProviderEnabled != true || isNetworkProviderEnabled!=true)
throw new HighAccuracyGPSNeeded();
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, locationListener);
return locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment