权限

    private void initData() {
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) ==
                PackageManager.PERMISSION_GRANTED){
            readSD();
        }else{
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1:
                if (grantResults.length>0&&grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    readSD();
                }else{
                    Toast.makeText(this,"没有权限",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void readSD() {
        list.addAll(SDutils.getMp3(this));
        adapter.notifyDataSetChanged();
    }

危险权限

权限_第1张图片
image.png

处理多个权限

String[] per = new String[]{
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.CAMERA
        };

        ActivityCompat.requestPermissions(this, per, 100);

你可能感兴趣的:(权限)