- 网络定位权限为
Manifest.permission.ACCESS_COARSE_LOCATION
在Activity中检查
if (Build.VERSION.SDK_INT >= 23) {
int checkPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
//没有获取权限,发起申请
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
} else {
//doing everything what you want
}
}
在Activity中重写以下方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//发起定位
initLocation();
} else {
ToastUtils.showShortToast("您拒绝了定位权限");
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
在Fragment中检查权限
- fragmetn中发起权限申请与Activity操作有一点不同,不过也不容忽视,若是不小心手一抖写错了方法,是不会执行回调方法的........
if (Build.VERSION.SDK_INT >= 23) {
int checkPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
//没有获取权限,发起申请,不同之处
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
} else {
//doing everything what you want
}
}
- 在fragment中重写
onRequestPermissionsResult()
方法,和Activity中做法一致
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//发起定位
initLocation();
} else {
ToastUtils.showShortToast("您拒绝了定位权限");
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
博客
其他权限后面持续更新.......,欢迎 关注