动态申请权限

//动态申请读写sd卡权限

intresult = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

if(result != PackageManager.PERMISSION_GRANTED) {

//申请权限

ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);

}

申请权限结果回调:

@Override

public voidonRequestPermissionsResult(intrequestCode,@NonNullString[] permissions,@NonNullint[] grantResults) {

//super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch(requestCode) {

case0:

if(grantResults[0]==PackageManager.PERMISSION_DENIED) {

Toast.makeText(MainActivity.this,"权限被拒绝", Toast.LENGTH_SHORT).show();

}else{

//创建目录

DownloadManager.getInstance().createFileDir();

}

break;

}

}

你可能感兴趣的:(动态申请权限)