Android &引用;位置提供商要求访问“位置许可”;即使我使用运行时请求权限
这太奇怪了。我们都知道,如果Android &引用;位置提供商要求访问“位置许可”;即使我使用运行时请求权限,android,gps,locationmanager,targetsdkversion,Android,Gps,Locationmanager,Targetsdkversion,这太奇怪了。我们都知道,如果设置为23+,除了在清单中声明之外,还必须请求运行时权限 多年来,这项工作一直没有任何问题。现在的问题是,当我将targetSdkVersion设置为“26”时,LocationManager不再工作!并抛出以下错误: "gps" location provider requires ACCESS_FINE_LOCATION permission 我在我的应用程序中唯一改变的是targetsdkversion从23到26,它破坏了应用程序!有什么想法吗?试试这个完整
设置为23+,除了在清单中声明
之外,还必须请求运行时权限
多年来,这项工作一直没有任何问题。现在的问题是,当我将targetSdkVersion设置为“26”时,LocationManager不再工作!并抛出以下错误:
"gps" location provider requires ACCESS_FINE_LOCATION permission
我在我的应用程序中唯一改变的是targetsdkversion从23到26,它破坏了应用程序!有什么想法吗?试试这个完整的解决方案 在清单中添加权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
此方法checkAndroidVersion()代码>
此方法checkAndRequestPermissions()
声明此方法buildClient()
句柄onStop()
声明此方法checkPlayServices()
重写googleapi方法
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
startLocationUpdates();
mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocation == null) {
startLocationUpdates();
}
if (mLocation != null) {
double latitude = mLocation.getLatitude();
double longitude = mLocation.getLongitude();
} else {
// Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
}
/**
* This library works in release mode only with the same JKS key used for
* your Previous Version
*/
}
protected void startLocationUpdates() {
// Create the location request
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(5000)
.setFastestInterval(5000);
// Request location updates
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
Log.d("reque", "--->>>>");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection Suspended");
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
try {
if (checkAndRequestPermissions()) {
buildClient();
mGoogleApiClient.connect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
current_location = location;
SharedPrefsUtils.setStringPreference(MainActivity.this, "user_current_latitude", current_location.getLatitude() + "");
SharedPrefsUtils.setStringPreference(MainActivity.this, "user_current_longitude", current_location.getLongitude() + "");
System.out.println("sjkbd jdsbj");
}
在活动中
或片段中
public void buildClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
好的,我可以解决这个问题,我会在这里解释,以防其他人遇到类似的问题。我应该补充一点,虽然这个问题现在已经解决了,但感觉像是Android上的一个bug或者缺少文档
由于一些内部项目原因,我请求如下位置许可:
PackageInfo packageInfo = pm.getPackageInfo(_activity.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = null;
if (packageInfo != null)
{
requestedPermissions = packageInfo.requestedPermissions;
if (requestedPermissions.length > 0)
{
List<String> requestedPermissionsList = Arrays.asList(requestedPermissions);
_requestedPermissionsArrayList = new ArrayList<String>();
_requestedPermissionsArrayList.addAll(requestedPermissionsList);
}
}
for(int i=0; i < _requestedPermissionsArrayList.size(); i++)
{
if(_requestedPermissionsArrayList.get(i).equals(Manifest.permission.ACCESS_FINE_LOCATION) || // api level 1
_requestedPermissionsArrayList.get(i).equals(Manifest.permission.ACCESS_COARSE_LOCATION) // api level 1
)
{
isFound = true;
ActivityCompat.requestPermissions(_activity, new String[]{
_requestedPermissionsArrayList.get(i)
}, ExConsts.MY_PERMISSIONS_REQUEST);
break;
}
}
现在,无论清单中的权限顺序如何,它都可以正常工作。干杯。您在清单上声明了权限并请求了运行时权限了吗?@Ragesh是的,这就是为什么它如此奇怪的原因。您可以发布您的日志吗?
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.d("in main on request", "Permission callback called-------");
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.d("in main on request", "location services permission granted");
// process the normal flow
//else any one or both the permissions are not granted
buildClient();
mGoogleApiClient.connect();
} else {
Log.d("in fragment on request", "Some permissions are not granted ask again ");
//permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
// // shouldShowRequestPermissionRationale will return true
//show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
showDialogOK("Location services services are required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
Toast.makeText(MainActivity.this, "Go to settings and enable permissions", Toast.LENGTH_LONG)
.show();
// //proceed with logic by disabling the related features or quit the app.
}
}
}
}
}
}
public void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
public void buildClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void onStop() {
super.onStop();
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected() && mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
}
}
/**
* Method to verify google play services on the device
*/
public boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(MainActivity.this,
"This device is not supported.", Toast.LENGTH_LONG)
.show();
finish();
}
return false;
}
return true;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
startLocationUpdates();
mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocation == null) {
startLocationUpdates();
}
if (mLocation != null) {
double latitude = mLocation.getLatitude();
double longitude = mLocation.getLongitude();
} else {
// Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
}
/**
* This library works in release mode only with the same JKS key used for
* your Previous Version
*/
}
protected void startLocationUpdates() {
// Create the location request
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(5000)
.setFastestInterval(5000);
// Request location updates
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
Log.d("reque", "--->>>>");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection Suspended");
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
try {
if (checkAndRequestPermissions()) {
buildClient();
mGoogleApiClient.connect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
current_location = location;
SharedPrefsUtils.setStringPreference(MainActivity.this, "user_current_latitude", current_location.getLatitude() + "");
SharedPrefsUtils.setStringPreference(MainActivity.this, "user_current_longitude", current_location.getLongitude() + "");
System.out.println("sjkbd jdsbj");
}
private GoogleApiClient mGoogleApiClient;
private Location mLocation;
private LocationManager locationManager;
private LocationRequest mLocationRequest;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
PackageInfo packageInfo = pm.getPackageInfo(_activity.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = null;
if (packageInfo != null)
{
requestedPermissions = packageInfo.requestedPermissions;
if (requestedPermissions.length > 0)
{
List<String> requestedPermissionsList = Arrays.asList(requestedPermissions);
_requestedPermissionsArrayList = new ArrayList<String>();
_requestedPermissionsArrayList.addAll(requestedPermissionsList);
}
}
for(int i=0; i < _requestedPermissionsArrayList.size(); i++)
{
if(_requestedPermissionsArrayList.get(i).equals(Manifest.permission.ACCESS_FINE_LOCATION) || // api level 1
_requestedPermissionsArrayList.get(i).equals(Manifest.permission.ACCESS_COARSE_LOCATION) // api level 1
)
{
isFound = true;
ActivityCompat.requestPermissions(_activity, new String[]{
_requestedPermissionsArrayList.get(i)
}, ExConsts.MY_PERMISSIONS_REQUEST);
break;
}
}
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
ActivityCompat.requestPermissions(_activity, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
}