申请函数:
// 申请码,回调的时候会用到
private final int location_requestCode = 0xb01;
// 如果版本大于 Build.VERSION_CODES.M(23),才需要动态申请
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, location_requestCode);
}
}
申请结果的回调函数:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case location_requestCode:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
break;
}
}
网友评论