使用三方框架
compile 'com.lovedise:permissiongen:0.0.6
//1.请求权限
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PermissionGen.with(MainActivity.this)
.addRequestCode(100)
.permissions(
Manifest.permission.READ_CONTACTS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.WRITE_CONTACTS)
.request();
}
});
/**
* 授权成功,后处理的逻辑
*/
@PermissionSuccess(requestCode = 100)
public void doSomething() {
Toast.makeText(this, "Contact permission is granted", Toast.LENGTH_SHORT).show();
}
/**
* 授权失败,后处理的逻辑
*/
@PermissionFail(requestCode = 100)
public void doFailSomething() {
Toast.makeText(this, "Contact permission is not granted", Toast.LENGTH_SHORT).show();
PermissionGen.with(MainActivity.this)
.addRequestCode(100)
.permissions(
Manifest.permission.READ_CONTACTS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.WRITE_CONTACTS)
.request();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
PermissionGen.onRequestPermissionsResult(MainActivity.this, requestCode, permissions, grantResults);
}
需要在清单文件中先声明好对应的权限,READ_CONTACTS,RECEIVE_SMS,WRITE_CONTACTS
网友评论