Created
November 1, 2018 22:20
-
-
Save tanerjn/886334bcab98ce8ea6d0352c02704c45 to your computer and use it in GitHub Desktop.
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 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