其实也没方便多少,主要是自己用起来舒服些。PermissionUtils类中的PermissionDialog是我自己的实现,如果不需要可以删掉,或者替换成自己的,PermissionAdapter是为了配合Dialog,不需要可以删掉。
如果你的项目中有BaseActivity类,覆写onRequestPermissionsResult处理权限请求结果,并实现PermissionListener接口,参考第二部分。如果没有BaseActivity类,建议有一个就直接覆写onRequestPermissionsResult。
效果:
有弹窗
public class PermissionUtils {
public static final int REQUEST_PERMISSION = 1001;
/**
* 此方法直接请求权限
* @param activity 需要申请权限的Activity
* @param permissions 请求的权限列表
*/
public static void request(BaseActivity activity, String[] permissions){
List<String> list = check(permissions);
if(list.size() != 0){
ActivityCompat.requestPermissions(activity, list.toArray(new String[list.size()]), REQUEST_PERMISSION);
}
}
/**
* 此方法会弹出一个对话框,表明需要哪些权限,以及权限的作用
* @param context 需要申请权限的Context
* @param permissionDescription Map的key为权限名(非严格,可以自己随便起名),value为权限作用
* @param permissions 请求的权限列表
*/
public static void request(Context context,Map<String,String> permissionDescription,String[] permissions){
if(needRequest(permissions)){
PermissionDialog dialog = new PermissionDialog(context);
dialog.setCancelable(false);
dialog.setPermissionMap(permissionDescription);
dialog.setRequest(permissions);
dialog.show();
}
}
/**
* 返回需要申请的权限列表,过滤掉已经申请过的权限
* @param permissions
* @return
*/
private static List<String> check(String[] permissions){
List<String> list = new ArrayList<>();
for(String p:permissions){
if (ContextCompat.checkSelfPermission(App.getApplication(),p) != PackageManager.PERMISSION_GRANTED){
//说明没有权限
list.add(p);
}
}
return list;
}
/**
* 检查是否需要弹窗
* @param permissions
* @return
*/
private static boolean needRequest(String[] permissions){
for(String p:permissions){
if (ContextCompat.checkSelfPermission(App.getApplication(),p) != PackageManager.PERMISSION_GRANTED){
//说明没有权限
return true;
}
}
return false;
}
/**
* 此Dialog只会在申请权限时弹出,不会在其他时候使用
*/
static class PermissionDialog extends BaseDialog implements View.OnClickListener{
TextView tv_commit;
String[] permissions;
ViewPager vp_permission;
PermissionAdapter adapter;
public PermissionDialog(@NonNull Context context) {
super(context);
addToManager();
}
@Override
protected int getResourceId() {
return R.layout.dialog_permission2;
}
@Override
protected void initView() {
vp_permission = $(R.id.vp_permission);
tv_commit = $(R.id.tv_commit);
tv_commit.setOnClickListener(this);
}
@Override
protected void initData() {
}
@Override
public void reset() {
}
@Override
public void onClick(View v) {
dismiss();
if(permissions != null && permissions.length !=0){
PermissionUtils.request((BaseActivity) context,permissions);
}
}
public void setPermissionMap(Map<String,String> map){
adapter = new PermissionAdapter(context,map);
vp_permission.setAdapter(adapter);
}
public void setRequest(String[] permissions){
this.permissions = permissions;
}
}
static class PermissionAdapter extends PagerAdapter{
private Context context;
private List<String> permissionName;
private List<String> permissionDescription;
private List<View> roots = new ArrayList<>();
public PermissionAdapter(Context context,Map<String,String> map){
this.context = context;
split(map);
}
private void split(Map<String,String> map){
if(map != null && map.size() > 0){
Set<String> key = map.keySet();
permissionName = new ArrayList<>(key);
permissionDescription = new ArrayList<>();
for(String k:permissionName){
permissionDescription.add(map.get(k));
}
}
}
@Override
public int getCount() {
return permissionName == null ? 0 : permissionName.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(roots.get(position));
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View root = LayoutInflater.from(context).inflate(R.layout.fragment_permission,container,false);
TextView tv_permission_name = root.findViewById(R.id.tv_permission_name);
TextView tv_permission_description = root.findViewById(R.id.tv_permission_description);
tv_permission_name.setText(permissionName.get(position));
tv_permission_description.setText(permissionDescription.get(position));
roots.add(root);
container.addView(root);
return root;
}
}
public interface PermissionListener{
void onPermissionResult(List<String> allow,List<String> deny);
}
}
在BaseActivity中,实现PermissionListener接口,并处理权限回调
public abstract class BaseActivity extends AppCompatActivity implements PermissionUtils.PermissionListener{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
initView(savedInstanceState);
initData(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
DialogManager.free(this);
}
/**
* 动态申请权限的时候,如果需要监听申请结果,覆写次此方法即可
* @param allow
* @param deny
*/
@Override
public void onPermissionResult(List<String> allow, List<String> deny) {
}
abstract protected int getContentViewId();
abstract protected void initView(Bundle savedInstanceState);
abstract protected void initData(Bundle savedInstanceState);
public <V extends View> V $(int id){
return (V)findViewById(id);
}
public Drawable findDrawableById(int id){
return ContextCompat.getDrawable(this,id);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == PermissionUtils.REQUEST_PERMISSION){
List<String> allow = new ArrayList<>();
List<String> deny = new ArrayList<>();
for(int i=0; i < permissions.length; i++){
if(grantResults[i] == PERMISSION_GRANTED){
allow.add(permissions[i]);
}else{
deny.add(permissions[i]);
}
}
onPermissionResult(allow,deny);
}
}
}
使用
public class MainActivity extends BaseActivity {
@Override
protected int getContentViewId() {
return R.layout.activity_main;
}
@Override
protected void initView(Bundle savedInstanceState) {
}
@Override
protected void initData(Bundle savedInstanceState) {
Map<String,String> map = new HashMap<>();
map.put("定位","获取您的位置信息");
map.put("传感器","获取您的运动信息");
map.put("读写存储","保存您记录的信息和加载已保存的信息");
PermissionUtils.request(this,map,new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BODY_SENSORS,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE});
}
@Override
public void onPermissionResult(List<String> allow, List<String> deny) {
super.onPermissionResult(allow, deny);
ToastUtils.show("已同意"+allow.size()+"个权限,未同意"+deny.size()+"个权限");
}
}
说明:Utils中弹出的Dialog样式自己定义实现即可。
网友评论