这是一个全局的封装类,推荐使用于MainActicity一次性请求所有权限(局部的权限请求待补充)
android 6.0需要我们动态的申请权限,比如内存卡读写的权限
这里在网上找了一个封装的类:
public class PermisionUtils {
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {//这里只加入了读写和相机权限,还可以加入其他权限
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
/**
* Checks if the app has permission to write to device storage
* If the app does not has permission then the user will be prompted to
* grant permissions
* <p>
* * @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
for (String per : PERMISSIONS_STORAGE) {
int permission = ActivityCompat.checkSelfPermission(activity,
per);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
break;
}
}
}
}
用法:
在需要申请权限的activity onCreat中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
PermisionUtils.verifyStoragePermissions(this);
......
}
//用户授权或者拒绝授权之后的操作
onRequestPermissionsResult()
网友评论