Android: Permission to use Geolocation
There is an application using geolocation. It has a check for on/off geolocation permission. And if geolocation is disabled, it sends it to the phone settings in this way:
public void onClickLocationSettings() {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
But when testing on Android 8.0, it turned out that this is not the same setting and geolocation for the app is already enabled in a different place.
Question: how do I universally refer to "geolocation permission for my specific application"?
2
3 answers
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// делаем что-то, если геолокация не включена
}
And here is the code to prompt the user to enable geolocation
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
2
Author: Wlad, 2019-03-27 10:17:22
I will add a little, I have a problem on Android 10 that the service does not get access to the location in the background. Add to the manifest
<service
android:foregroundServiceType="location"/>
And in the permissions
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
1
Author: Arty Morris, 2021-01-09 12:14:50
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.ACCESS_COARSE_LOCATION )) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION },
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}} else {
// Permission has already been granted}
//and need to add to the manifest
Https://developer.android.com/training/permissions/requesting#java
-2
Author: JunDev, 2019-03-27 08:14:20