我有一个应用程序,我想通过在打开活动“地图”时放下一个别针来向用户显示位置。我还想显示用户的位置,如果从附加信息中提供的信息出现问题,地图将以用户位置为中心。Android运行时位置服务权限
与其他许多人一样,我有一天注意到我的应用没有显示用户的位置高于Android 6.0。所以我开始搜索SO和Google,并找到了一些线程和教程。原来,我必须检查权限是否已经可用或者是否需要通过调用方法checkSelfPermissions来授予,如果它们没有被授予,我必须通过调用requestPermissions来请求它们。当他们被请求时,我想设置标记并显示用户的位置(或者如果某件事与来自额外设备的标记信息混淆在一起,请将它放在中心位置)。我想用我的方法setUpMarker来做到这一点。下面是我onMapReady(称为图时已准备就绪)和onRequestPermissionsResult(当从许可请求的用户响应调用)方法:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Permission is not yet available and needs to be asked for
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
// We provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
new AlertDialog.Builder(Map.this)
.setMessage("To show the location the permission is needed")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(Map.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_ID);
}
})
.setNegativeButton("Cancel", null)
.create()
.show();
} else {
// Permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_REQUEST_ID);
}
} else {
// Permission is already available
setUpMarker();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_ID: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// The permission request was granted, we set up the marker
setUpMarker();
} else {
// The permission request was denied, we make the user aware of why the location is not shown
Toast.makeText(this,"Since the permission wasn't granted we can't show the location",Toast.LENGTH_LONG).show();
}
return;
}
}
}
现在,我仍然在我的setUpMarker得到一个错误方法当我想启用用户的位置,当我想要在用户的位置中心地图。我得到的错误是这样的:
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with 'checkPermission') or explicitly handle a potential 'SecurityException'.
但我已经检查过权限!这里是我的setUpMarker:
private void setUpMarker() {
//HERE I GET THE ERROR
mMap.setMyLocationEnabled(true);
if(!getIntent().getExtras().isEmpty()) {
// Latitude and longitude was retrieved successfully, set up marker
...
} else {
// Latitude and longitude was not retrieved, zoom to current location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// AND HERE I GET THE ERROR TOO
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.zoom(17)
.bearing(90)
.tilt(40)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
我该如何让应用程序记住权限,或以其他方式解决此问题?我已经检查过类似的线程,但没有一个似乎有这种问题。
好吧!另外一点:ContextCompat和ActivityCompat有什么区别? – jahed
就像Activity扩展Context一样,ActivityCompat扩展了ContextCompat并添加了Activity特定的方法。 'checkSelfPermission'恰好只需要一个Context而不是一个Activity – ianhanniballake