Android 权限简单分装
程序员文章站
2022-05-14 21:21:59
...
Android 6.0 权限简单封装
Android 提供了 如下函数提供给开发者使用.
1 . ActivityCompat.checkSelfPermission(…)
检测是否具备权限
2 .ActivityCompat.requestPermissions(…);
向用户请求权限
3 .onRequestPermissionsResult
监听用户授权回调.
实际开发中.有很多动态权限需要申请.还要等用户把这些权限都授予后才能进行如下操作.所以我就进行了一个简单的封装.下面这段代码是可用的.(V)! .
工具类.
public class PermissionRequest {
int mRequestCode;
OnRequested mReques;
String[] mPermissions; //必要
String[] mPermissionsSecondary; //次要
Activity mActivity;
public PermissionRequest(Activity activity,int requestCode,String[] permission1, String[] permission2,OnRequested mReques) {
this.mRequestCode = requestCode;
this.mReques = mReques;
this.mPermissions = (permission1 == null) ? new String[]{}: permission1;
this.mPermissionsSecondary = (permission2 == null) ? new String[]{}: permission2;
this.mActivity = activity;
}
public void pause(){
mActivity = null;
}
public void request() {
ArrayList<String> requestP = new ArrayList<>(); //未申请的必要权限
ArrayList<String> requestS = new ArrayList<>(); //未申请的非必要权限
checkRequestError(requestP,mPermissions);
checkRequestError(requestS,mPermissionsSecondary);
if (requestP.size() == 0) {
if (requestS.size() == 0){
mReques.onPerRequestedAllSucess();
return;
}
mReques.onPerRequestedSucess(requestS.toArray(new String[]{}));
} else {
ArrayList<String> list = new ArrayList<>();
list.addAll(requestP);
list.addAll(requestS);
ActivityCompat.requestPermissions(mActivity, list.toArray(new String[]{}), mRequestCode);
}
}
private void checkRequestError(ArrayList<String> request,String[] permissions) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(mActivity, permission) == -1) {
request.add(permission);
}
}
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (mRequestCode == requestCode) {
request();
}
}
public interface OnRequested {
void onPerRequestedAllSucess(); //必要和 非必要的权限 全部申请成功
/**
* 所有必须的权限申请成功
* permission 没有未申请成功的非要权限
*/
void onPerRequestedSucess(String[] permissions);
}
}
权限Activity类(基类)
public abstract class PermisActivity extends AppCompatActivity implements PermissionRequest.OnRequested {
private String[] mPermissions_storage;
private String[] mPermissions_storage_Secondary;
//oncreate里面设置
//必要权限
public void setNecessaryPermissionsStorages(String ... permissions_storage){
mPermissions_storage = permissions_storage;
};
//非必要权限
public void setNot_Essential(String ... permissions_storage){
mPermissions_storage_Secondary = permissions_storage;
}
@CallSuper
@Override
public void onPostCreate( Bundle savedInstanceState, PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
}
protected void checkPermiss() {
mPermissionRequest = new PermissionRequest(this,12,mPermissions_storage,mPermissions_storage_Secondary,this);
mPermissionRequest.request();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
mPermissionRequest.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
@Override
protected void onDestroy() {
super.onDestroy();
mPermissionRequest.pause();
}
PermissionRequest mPermissionRequest;
}
我写到基类里面的原因是.比较闲 不太建议这样做. 当然如果每个界面都要动态权限也是可以这样玩的.
//必要权限用户必须全部同意否则就会一直弹窗口.
public class MainActivity extends PermisActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.tv_perssion);
//必要权限
setNecessaryPermissionsStorages(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.READ_CONTACTS
);
//非必要权限
setNot_Essential(Manifest.permission.READ_PHONE_STATE);
checkPermiss();
}
TextView mTextView;
Handler handler = new Handler();
@Override
public void onPerRequestedAllSucess() {
mTextView.setText("所有权限申请成功");
}
@Override
public void onPerRequestedSucess(final String[] permissions) {
mTextView.setText("所有必要权限申请完成 还有"+ permissions.length+"个非必要权限未申请");
}
}