Android一行代码权限的更优解决方案:
dependencies {
implementation 'com.qw:soulpermission:1.1.3'
}
基本用法:
SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
//if you want do noting or no need all the callbacks you may use SimplePermissionAdapter instead
new CheckRequestPermissionListener() {
@Override
public void onPermissionOk(Permission permission) {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
"\n is ok , you can do your operations", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(Permission permission) {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
" \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
}
});
SoulPermission.getInstance().checkAndRequestPermissions(
Permissions.build(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
//if you want do noting or no need all the callbacks you may use SimplePermissionsAdapter instead
new CheckRequestPermissionsListener() {
@Override
public void onAllPermissionOk(Permission[] allPermissions) {
Toast.makeText(ApiGuideActivity.this, allPermissions.length + "permissions is ok" +
" \n you can do your operations", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(Permission[] refusedPermissions) {
Toast.makeText(ApiGuideActivity.this, refusedPermissions[0].toString() +
" \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
}
});
SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.READ_CONTACTS,
new CheckRequestPermissionListener() {
@Override
public void onPermissionOk(Permission permission) {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
"\n is ok , you can do your operations", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(Permission permission) {
// see CheckPermissionWithRationaleAdapter
if (permission.shouldRationale()) {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
" \n you should show a explain for user then retry ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
" \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
}
}
});
//you can also use checkPermissions() for a series of permissions
Permission checkResult = SoulPermission.getInstance().checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION);
boolean checkResult = SoulPermission.getInstance().checkSpecialPermission(Special.NOTIFICATION);
SoulPermission.getInstance().goPermissionSettings();
SoulPermission.setDebug(true);
注意事项:
//invoke init in your application when auto init failed
public class SimpleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//no necessary
SoulPermission.init(this);
}
}
工作原理和最佳示例
项目参考地址:https://github.com/soulqw/SoulPermission