我使用此功能来授予用户许可:
None
我将此代码添加到Manifest
RegisterGradient
我在onCreate()中调用了它:
private void askForPermission(String permission, Integer requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
}
} else {
Toast.makeText(this,"we have", Toast.LENGTH_SHORT).show();
}
}
用户允许,但当我在下一行检查时,返回给我权限否认! 我想给出当前的位置。 我的错在哪里?
答案 0 :(得分:0)
当用户点击允许或拒绝按钮时,你需要检查它调用的onRequestPermissionsResult()的权限
final String[] NECESSARY_PERMISSIONS = new String[] {
Manifest.permission.ACCESS_FINE_LOCATION
};
if ((ContextCompat.checkSelfPermission(DialerHomeActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
//Granted
} else {
// ask for permission
ActivityCompat.requestPermissions(DialerHomeActivity.this,
NECESSARY_PERMISSIONS, 123);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 123:
if (grantResults.length > 0) {
if ((grantResults[0] == PackageManager.PERMISSION_GRANTED))) {
//Permission granted
} else {
if ((grantResults[0] == PackageManager.PERMISSION_DENIED && !ActivityCompat
.shouldShowRequestPermissionRationale(
getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION))) {
//Permission deny with never ask again
} else {
//Permission deny
}
}
}
}
}