在我的活动的onCreate()中,我检查权限。
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CHECK_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
}
对于每个权限,在其回调方法中我执行某些任务。所以我不能在数组中询问权限,如
String[] permissions { };
因为它只为我提供一个所有权限的回调。我按以下方式在每个回调中执行任务。
case ConstantClass.READ_PERMISSION:
if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
doSomething();
Toast.makeText(MainActivity.this, getResources().getString(R.string.Cannot_Vibrate_Phone), Toast.LENGTH_LONG).show();
}
break;
case ConstantClass.CALL_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
dialPhoneNumber();
} else {
Toast.makeText(MainActivity.this, getString(R.string.permission_access_call), Toast.LENGTH_LONG).show();
}
break;
如果我在onCreate()中一个接一个地询问这些权限。只显示第一个权限。所以我只有一个回拨工作。那么如何实现这样的场景:对于每个单独的权限,我得到一个单独的回调并调用其中的一些函数。 谢谢:)
答案 0 :(得分:1)
检查此演示代码
在第一次许可拒绝/授权的回应中询问下一个许可
onRequestPermissionsResult()
public class MainActivity extends Activity {
private Context context;
private static final int REQUEST_RUNTIME_PERMISSION_1 = 111;
private static final int REQUEST_RUNTIME_PERMISSION_2 = 222;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
if (CheckPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// you have permission go ahead
} else {
// you do not have permission go request runtime permissions
RequestPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION_1);
}
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions,
int[] grantResults) {
switch (permsRequestCode) {
case REQUEST_RUNTIME_PERMISSION_1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// you have permission go ahead ask next permission and get separate callback
RequestPermission(MainActivity.this, Manifest.permission.CAMERA, REQUEST_RUNTIME_PERMISSION_2);
} else {
// you do not have permission show toast.
/*check your logic ask again*/
//RequestPermission(MainActivity.this, Manifest.permission.CAMERA, REQUEST_RUNTIME_PERMISSION_2);
}
return;
}
case REQUEST_RUNTIME_PERMISSION_2: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// you have 2nd permission go ahead
} else {
// you do not have 2nd permission show toast.
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}
答案 1 :(得分:1)
您可以要求onRequestPermission
中的下一个权限一个接一个地显示权限请求 。您可以检查以下代码并获得想法。在onRequestPermissionResult
方法中:
case ConstantClass.READ_PERMISSION:
if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
doSomething();
Toast.makeText(MainActivity.this, getResources().getString(R.string.Cannot_Vibrate_Phone), Toast.LENGTH_LONG).show();
}
// Ask for Camera permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CHECK_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
break;
希望这有帮助。
答案 2 :(得分:0)
String permissions[] = new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.INTERNET};
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
&&ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&&ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ){
ActivityCompat.requestPermissions(this, permissions, ALL_REQUEST_CODE);
}
else if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&&ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[] {permissions[1], permissions[2], permissions[3]}, ALL_REQUEST_CODE);
}
else if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[] {permissions[2], permissions[3]}, ALL_REQUEST_CODE);
}
这是获取权限的最佳方法