Created
April 30, 2017 19:54
-
-
Save mkantar/a3bb284fbbbe2c21052c5ec20dd64efe to your computer and use it in GitHub Desktop.
Location provider service
This file contains 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 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