美文网首页
封装运行时权限

封装运行时权限

作者: 加油小李 | 来源:发表于2020-08-04 22:52 被阅读0次

一,创建工具类

0 1 2

import android.app.Activity;

import android.content.pm.PackageManager;

import android.os.Build;

import android.support.annotation.NonNull;

import android.support.annotation.RequiresApi;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class PermissionUtils {

private ActivitymActivity;

    private int mReqCode;

    private CallBackmCallBack;

    public static interface CallBack {

void grantAll();

        void denied();

    }

public PermissionUtils(Activity activity) {

mActivity = activity;

    }

@RequiresApi(api = Build.VERSION_CODES.M)

public void request(List needPermissions, int reqCode, CallBack callback) {

if (Build.VERSION.SDK_INT <23) {

callback.grantAll();

return;

        }

if (mActivity ==null) {

throw new IllegalArgumentException("activity is null.");

        }

mReqCode = reqCode;

        mCallBack = callback;

        List reqPermissions =new ArrayList<>();

        for (String permission : needPermissions) {

if (mActivity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {

reqPermissions.add(permission);

            }

}

// fixed!!!

        if (reqPermissions.isEmpty()) {

callback.grantAll();

return;

        }

mActivity.requestPermissions(reqPermissions.toArray(new String[]{}), reqCode);

    }

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

if (requestCode ==mReqCode) {

boolean grantAll =true;

            for (int i =0; i < grantResults.length; i++) {

if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {

grantAll =false;

                    Toast.makeText(mActivity, permissions[i] +" 未授权", Toast.LENGTH_SHORT).show();

break;

                }

}

if (grantAll) {

mCallBack.grantAll();

            }else {

mCallBack.denied();

            }

}

}

}

二 ,Activity中调用该工具类

3 4

import android.Manifest;

import android.content.Intent;

import android.os.Build;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.annotation.RequiresApi;

import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;

import java.util.List;

public class SplashActivity extends AppCompatActivity {

public static List<String> NeedReqPermissions =new ArrayList<>();

    static {

sNeedReqPermissions.add(Manifest.permission.READ_PHONE_STATE);

        sNeedReqPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);

        sNeedReqPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION);

        sNeedReqPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);

    }

private PermissionUtilsmPermissionUtils;

    @RequiresApi(api = Build.VERSION_CODES.M)

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);

        mPermissionUtils =new PermissionUtils(this);

        mPermissionUtils.request(sNeedReqPermissions, 100, new PermissionUtils.CallBack() {

@Override

            public void grantAll() {

toMainActivity();

                finish();

            }

@Override

            public void denied() {

finish();

            }

});

    }

private void toMainActivity() {

Intent intent =new Intent(this, MainActivity.class);

        startActivity(intent);

    }

@Override

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        mPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

}

相关文章

网友评论

      本文标题:封装运行时权限

      本文链接:https://www.haomeiwen.com/subject/jqpgrktx.html