美文网首页
动态申请权限

动态申请权限

作者: Braveheart丶Ws | 来源:发表于2017-07-18 23:04 被阅读2次

//动态申请读写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;

}

}

相关文章

网友评论

      本文标题:动态申请权限

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