int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permissionCheck != PackageManager.PERMISSION_GRANTED) {
//没有权限
Toast.makeText(MainActivity.this, "you've not got permissions", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(MainActivity.this,permissions,50);
}
else {
//有权限
Toast.makeText(MainActivity.this, "you've already got permissions", Toast.LENGTH_LONG).show();
}
private static String[] permissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// TODO Auto-generated method stub
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Toast.makeText(MainActivity.this, "permissions", Toast.LENGTH_LONG).show();
if(requestCode == 50) {
for(int i = 0;i < permissions.length;i++) {
System.out.println("申请的权限为:" + permissions[i] + ",申请结果:" + grantResults[i]);
}
}
}