如何在Android Studio 中检测手机中的其他权限?

qni6mghb  于 2023-02-02  发布在  Android
关注(0)|答案(1)|浏览(116)
@Override
  protected void onCreate(Bundle savedInstanceState)
  {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

   mButton = findViewById(R.id.btncheck);
    mButton.setOnClickListener(new View.OnClickListener() {
       @Override
        public void onClick(View view)
       {
         Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
        intent.setClassName("com.miui.securitycenter",       "com.miui.permcenter.permissions.PermissionsEditorActivity");
        intent.putExtra("extra_pkgname", getPackageName());
        startActivity(intent);
           

       }
    });
}

它的代码只能导航和显示其他权限屏幕,但不能在需要权限的工作。
如何启用和取消其他权限在我的手机在android studio
Image here

dphi5xsq

dphi5xsq1#

您需要什么样的权限?
如果我没理解错的话,这应该会有帮助。
主要活动:

private final int REQUEST_CODE_PERMISSIONS = 1001;
    private final String[] REQUIRED_PERMISSIONS = new String[]{
            "android.permission.CAMERA",
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.RECORD_AUDIO",
            "android.permission.INTERNET",
            "android.permission.WRITE_EXTERNAL_STORAGE"};

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_PERMISSIONS) {
            if (!allPermissionsGranted()) {
                createToast(getString(R.string.error_permission));
                finish();
            }
        }
    }

private boolean allPermissionsGranted(){
        for(String permission : REQUIRED_PERMISSIONS){
            if(ContextCompat.checkSelfPermission(this , permission)
                    != PackageManager.PERMISSION_GRANTED){
                return false;
            }
        }
        return true;
    }

@Override
protected void onCreate(Bundle savedInstanceState) {
if(allPermissionsGranted()){
            Log.d("TAG", "OK");
        } else{
            ActivityCompat.requestPermissions(this,
                    REQUIRED_PERMISSIONS,
                    REQUEST_CODE_PERMISSIONS);
        }
...
}

相关问题