Android动态权限管理

时间:2021-11-21 15:19:02

做安卓其实挺费劲的,各种机型都够操心了,动态权限又恶心了一下Android动态权限管理


but 项目中针对6.0以上手机,还是必要处理一下权限申请的问题!!


首先,判断是否6.0以上,判断是否申请权限,拒绝了的权限怎么办?(android机型比较多,三星手机弹出申请弹框拒绝了,第二次还是会弹出权限申请框;但是我用的小米4,拒绝了第二次就默认拒绝了不会弹框;还有一些手机低版本的会一直弹框Android动态权限管理



权限工具类:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.PermissionChecker;

import com.motian.permissionrequest.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.targetSdkVersion;

/**
* ClassName:ParameterFactory
* Description TODO 动态权限处理
* 参考:http://blog.csdn.net/u011200604/article/details/52874599
* created by BAI
* Data 2016/12/20
*/
public class PermissionUtils {

private static final int PERMISSION_REQUEST_CODE = 100;

/**
* 请求权限
* @param mActivity
* @param permissions
*/
public void requestRunPermission(Activity mActivity, String[] permissions, PermissionListener mListener){
List<String> permissionLists = new ArrayList<>();
for(String permission : permissions){
if( selfPermissionGranted(mActivity,permission)){
permissionLists.add(permission);
}
}
if(!permissionLists.isEmpty()){
ActivityCompat.requestPermissions(mActivity, permissionLists.toArray(new String[permissionLists.size()]), PERMISSION_REQUEST_CODE);
}else{
//表示全都授权了
mListener.onGranted();
}
}

/**
* 判断权限是否已授权
* @param permission
* @return
*/
public boolean selfPermissionGranted(Activity mContext,String permission) {
// For Android < Android M, self permissions are always granted.
boolean result = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (targetSdkVersion >= Build.VERSION_CODES.M) {
// targetSdkVersion >= Android M, we can
// use Context#checkSelfPermission
result = ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED;
} else {
// targetSdkVersion < Android M, we have to use PermissionChecker
result = PermissionChecker.checkSelfPermission(mContext, permission) == PermissionChecker.PERMISSION_GRANTED;
}
}
return result;
}

/**
* 请求权限结果,baseActivity调用
* @param requestCode
* @param permissions
* @param grantResults
* @param mListener
*/
public void requestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults,PermissionListener mListener) {
switch (requestCode){
case PERMISSION_REQUEST_CODE:
if(grantResults.length > 0){
//存放没授权的权限
List<String> deniedPermissions = new ArrayList<>();
for(int i = 0; i < grantResults.length; i++){
int grantResult = grantResults[i];
String permission = permissions[i];
if(grantResult != PackageManager.PERMISSION_GRANTED){
deniedPermissions.add(permission);
}
}
if(deniedPermissions.isEmpty()){
//说明都授权了
mListener.onGranted();
}else{
mListener.onDenied(deniedPermissions);
}
}
break;
default:
break;
}
}

/**
* 打开设置权限页面
* @param activity
* @param message
*/
public static void openSettingActivity(final Activity activity, String message) {
showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivity(intent);
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
ToastUtils.showToast(activity,"请设置必要的权限!");
}
});
}

/**
* 显示弹框
* @param context
* @param message
* @param settingListener
* @param cancelListener
*/
private static void showMessageOKCancel(final Activity context, String message,
DialogInterface.OnClickListener settingListener,
DialogInterface.OnClickListener cancelListener) {
String permissionMessage = "当前应用缺少必要权限("+message+")\n" +
"\n 请点击“设置”-“权限”-打开所需权限。\n"+"" +
"\n 最后点击两次后退按钮,即可返回";
new AlertDialog.Builder(context)
.setTitle("提示")
.setMessage(permissionMessage)
.setPositiveButton("设置", settingListener)
.setNegativeButton("取消", cancelListener)
.create()
.show();

}
}

权限申请依附于Activity,BaseAcivity和BaseFragment调用:


/**
* ClassName: BaseActivity
* Description: 基础信息
* author 漠天
* date 2017/6/11
*/
public abstract class BaseActivity extends AppCompatActivity{
protected Activity mContext = this;
/**
* 权限监听
*/
private PermissionListener mListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

/**
* 请求权限(在onResume中写会重复调用弹窗,知道允许权限)
* @param mActivity
* @param permissions
*/
public void requestRunPermission(Activity mActivity, String[] permissions, PermissionListener mListener){
this.mListener = mListener;
new PermissionUtils().requestRunPermission(mActivity,permissions,mListener);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
new PermissionUtils().requestPermissionsResult(requestCode, permissions, grantResults,mListener);
}

}

/**
* ClassName:BaseFragment
* Description BaseFragment
* created by BAI
* Data 2017/6/11
*/
public abstract class BaseFragment extends Fragment{
/**
* Activity mContext
*/
protected Activity mContext;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mContext = getActivity();
return null;
}

/**
* 请求权限
* @param mActivity
* @param permissions
*/
public void requestRunPermission(Activity mActivity, String[] permissions, PermissionListener mListener){
((BaseActivity)mActivity).requestRunPermission(mActivity,permissions,mListener);
}

}


然后,MainActivity调用申请:

public class MainActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.request);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
permission();
}
});
Button openPermissionActivity = (Button)findViewById(R.id.openPermissionActivity);
openPermissionActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
intent.setData(uri);
mContext.startActivity(intent);
}
});

permission();
}

/**
* 判断权限
*/
private void permission(){
requestRunPermission(mContext,new String[]{Manifest.permission.CAMERA, Manifest.permission.CALL_PHONE,Manifest.permission.READ_EXTERNAL_STORAGE},new PermissionListener() {
@Override
public void onGranted() {
//表示所有权限都授权了
ToastUtils.showToast(mContext,"已经全部接受,可以处理其他事儿了^_^");
}

@Override
public void onDenied(List<String> deniedPermission) {
PermissionUtils.openSettingActivity(mContext,ToastUtils.getPermissionText(mContext,deniedPermission));
}
});
}

}

效果图:

Android动态权限管理




demo代码结构:

Android动态权限管理




demo PermissionRequest