当年写的代码太差劲了!!!!
逐一处理申请的权限,获取到权限成功后,才能申请下一个权限。如果调用addPermission(@NonNull String permissionName, @NonNull String tip, boolean isForce)方法, 第三个参数传入false时,则当前权限不是必须。
PermissionUtil.createBuilder()
.with(this)
//添加退出的回调
.setExitListener(new IExitListener() {
@Override
public void exit() {
}
})
.addPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.addPermission(Manifest.permission.CAMERA, Manifest.permission.CAMERA, false)
.addPermission(Manifest.permission.CALL_PHONE, Manifest.permission.CALL_PHONE)
.addPermission(Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_PHONE_STATE)
.addPermission(Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_PHONE_STATE)
.addPermission(Manifest.permission.READ_SMS, Manifest.permission.READ_SMS)
.build();
PermissionUtil.getInstance().requestPermissions();
/**
* @param permissionName 申请的 权限,必须是Manifest.permission中定义的
* @param tip 如果用户拒绝给该权限的提示
* @return
*/
public Builder addPermission(@NonNull String permissionName, @NonNull String tip) {
permissions.add(new Item(permissionName, null == tip ? permissionName : tip));
return this;
}
/**
* @param permissionName 申请的 权限,必须是Manifest.permission中定义的
* @param tip 如果用户拒绝给该权限的提示
* @param isForce 默认为true,权限必须要给,false 权限可忽略
* @return
*/
public Builder addPermission(@NonNull String permissionName, @NonNull String tip, boolean isForce) {
permissions.add(new Item(permissionName, null == tip ? permissionName : tip, isForce));
return this;
}
PermissionUtil.getInstance().requestPermissions();
//在onResume方法中,判断当点击不再询问后,进入设置界面再回到界面中时,是否需要继续弹出申请权限的框
@Override
protected void onResume() {
super.onResume();
boolean b = PermissionUtil.getInstance().enterSettingPage();
Log.i("onResume", "onResume = " + b);
if (b) {
PermissionUtil.getInstance().requestPermissions();
}
}
//配置申请权限的回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionUtil.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new PermissionUtil.Builder()
.setActivity(this)
//添加退出的回调
.setExitListener(new IExitListener() {
@Override
public void exit() {
}
})
.addPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.addPermission(Manifest.permission.CAMERA, Manifest.permission.CAMERA, false) //该权限可以不必须申请
.addPermission(Manifest.permission.CALL_PHONE, Manifest.permission.CALL_PHONE)
.addPermission(Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_PHONE_STATE)
.addPermission(Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_PHONE_STATE)
.addPermission(Manifest.permission.READ_SMS, Manifest.permission.READ_SMS)
.build();
PermissionUtil.getInstance().requestPermissions();
}
@Override
protected void onResume() {
super.onResume();
//是否选中了"不再询问",如果选中了不再询问,进入了设置界面,返回到首页时,是否提示申请权限
boolean b = PermissionUtil.getInstance().enterSettingPage();
Log.i("onResume", "onResume = " + b);
if (b) {
PermissionUtil.getInstance().requestPermissions();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//设置权限的回调
PermissionUtil.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}