Skip to content

Instantly share code, notes, and snippets.

@tanerjn
Created November 1, 2018 22:20
Show Gist options
  • Save tanerjn/886334bcab98ce8ea6d0352c02704c45 to your computer and use it in GitHub Desktop.
Save tanerjn/886334bcab98ce8ea6d0352c02704c45 to your computer and use it in GitHub Desktop.
public final Location getLocation(){
Location location = null;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
String mProvider = String.valueOf(locationManager.getBestProvider(criteria, true));
try{
getApplicationContext().getSystemService(LOCATION_SERVICE);
isEnabledNetwork = locationManager.isProviderEnabled(networkProvider);
isEnabledGPS = locationManager.isProviderEnabled(gpsProvider);
if(!isEnabledNetwork){
Log.d(NETWORK_UNABLE, "Should be enabled for location service.");
}
else if(!isEnabledGPS){
Log.d(GPS_UNABLE, "Should be enabled for location service.");
}
else if( !checkPermission() ){
ActivityCompat.requestPermissions(this, permissionsList , PERMISSIONS_REQUEST_MULTIPLE);
if( !checkPermission()){
Toast.makeText(this, "Location share is not permitted, application will not function as supposed to.", Toast.LENGTH_SHORT).show();
Log.i(REQUEST_DENIED, "Denial of location request.");
}
}
else{
if(locationManager != null){
location = locationManager.getLastKnownLocation(gpsProvider);
if(location != null)
Log.i(LOCATION_SERVICE, getTime()+"\t location update");
}
}
}catch(Exception err){
err.printStackTrace();
}
return location;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment