Reputation: 9926
I wrote simple code that return the gpsLocation of the device. When i compile it i get an error about that i did not used the 'checkSelfPermission' of access the GPS and Network.
When i get to the 'checkSelfPermission' code i get an exception.
why i get the exception ?
@TargetApi(Build.VERSION_CODES.M)
private void getLocation() {
try {
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled)
{
**// the exception is on the 'checkSelfPermission'**
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this);
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled)
{
if (location == null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this);
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
catch(Exception e)
{
Log.e("Error", e.getMessage());
}
}
Upvotes: 0
Views: 793
Reputation: 751
Directly from the android documentation :
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
System permissions are divided into two categories, normal and dangerous:
1.Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.
2.Dangerous permissions can give the app access to the user's confidential data.
If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. And accessing the device location needs a dangerous permission so you have to check and request for Location permission at runtime.
To check that your app has been granted permission, you can use:
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);
If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED
, and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED
, and the app has to explicitly ask the user for permission.
So you cannot avoid checkSelfPermission
.
Upvotes: 2