android - 请求获取位置的权限
问题描述
我正在按照这个问题的答案来解决我的问题,但问题是 onActivity 结果它总是转到 else 语句,并且没有出现用户可以允许权限的对话框。
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_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.
new AlertDialog.Builder(this)
.setTitle(R.string.title_location_permission)
.setMessage(R.string.text_location_permission)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(SalesPersonActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
})
.create()
.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Request location updates:
getLocation();
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
checkLocationPermission();
}
return;
}
}
解决方案
刚刚在 5 分钟前回答链接
使用通用代码请求权限,以便您在需要时在项目中的任何地方使用它。
你有两种方式
- 按照正确的方式请求许可。我正在发布我的代码片段,将此代码复制到您的 Base Activity 类中。
- 或者你使用RxPermissions。如果您是 Android 新手。
这是我的代码片段。
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
getBulkPermissions(permissions, new RequestPermissionAction() {
@Override
public void permissionDenied() {
// TODO: 5/27/2018 handle permission deny
}
@Override
public void permissionGranted() {
// TODO: 5/27/2018 you code do further operations
}
});
可以更简单吗?
现在,您将把这段代码 Base Activity 类放在您需要的任何地方。或者,如果您在任何其他地方不需要它,请加入您当前的课程。
RequestPermissionAction onPermissionCallBack;
private final static int REQUEST_BULK_PERMISSION = 3006;
private boolean checkBulkPermissions(String[] permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (String permission : permissions) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
} else {
return true;
}
}
public void getBulkPermissions(String[] permissions, RequestPermissionAction onPermissionCallBack) {
this.onPermissionCallBack = onPermissionCallBack;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!checkBulkPermissions(permissions)) {
requestPermissions(permissions, REQUEST_BULK_PERMISSION);
return;
}
}
if (onPermissionCallBack != null)
onPermissionCallBack.permissionGranted();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (onPermissionCallBack != null)
onPermissionCallBack.permissionGranted();
} else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
if (onPermissionCallBack != null)
onPermissionCallBack.permissionDenied();
}
}
public interface RequestPermissionAction {
void permissionDenied();
void permissionGranted();
}
还要检查您是否也需要ACCESS_COURSE_LOCATION
许可。
推荐阅读
- swift - 使用 URLSession.shared.dataTask Data Swift 解密 CommonCrypto 总是返回 nil
- c# - 不执行方法释放锁
- javascript - 如何在 nodeJS API 中自动化我的发布请求?
- tclsh - tclsh 相当于 bash 的 -c(运行命令)是什么?
- javascript - 如何使用 javascript vanilla 将数组与另一个元素进行比较
- netlogo - Netlogo - 为什么“if”语句被忽略?
- bash - 如何找到我使用 curl 命令下载的空文件?
- python - 如何使用通过我在 gurobi 中复制的模型变量定义的表达式添加二次约束?
- amazon-web-services - 在 AWS Redshift 中使用 COPY 命令时如何连接两列?
- javascript - 不能用向量的坐标来画椭圆