权限

作者: 君袅 | 来源:发表于2019-03-26 11:59 被阅读0次
        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();
        }
    

    危险权限

    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);
    
    

    相关文章

      网友评论

          本文标题:权限

          本文链接:https://www.haomeiwen.com/subject/mtlgvqtx.html