首先上工具类
/**
* Created by LH on 2016/7/27.
*/
public class PermissionUtil {
private static PermissionUtil permissionUtil = null;
private static List mListPermissions;
private static final String PERMISSIONS_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
private static final String PERMISSIONS_PHONE = Manifest.permission.READ_PHONE_STATE;
private static final String PERMISSIONS_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
private static final String PERMISSIONS_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private static final String PERMISSIONS_AUDIO = Manifest.permission.RECORD_AUDIO;
/**
* 添加权限
* author LH
* data 2016/7/27 11:27
*/
private void addAllPermissions(List mListPermissions) {
mListPermissions.add(PERMISSIONS_STORAGE);
mListPermissions.add(PERMISSIONS_PHONE);
mListPermissions.add(PERMISSIONS_ACCOUNTS);
mListPermissions.add(PERMISSIONS_LOCATION);
mListPermissions.add(PERMISSIONS_AUDIO);
}
/**
* 单例模式初始化
* author LH
* data 2016/7/27 11:27
*/
public static PermissionUtil getInstance() {
if (permissionUtil == null) {
permissionUtil = new PermissionUtil();
}
return permissionUtil;
}
/**
* 判断当前为M以上版本
* author LH
* data 2016/7/27 11:29
*/
public boolean isOverMarshmallow() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}
/**
* 获得没有授权的权限
* author LH
* data 2016/7/27 11:46
*/
@TargetApi(value = Build.VERSION_CODES.M)
public List findDeniedPermissions(Activity activity, List permissions) {
List denyPermissions = new ArrayList<>();
for (String value : permissions) {
if (activity.checkSelfPermission(value) != PackageManager.PERMISSION_GRANTED) {
denyPermissions.add(value);
}
}
return denyPermissions;
}
/**
* 获取所有权限
* author LH
* data 2016/7/27 13:37
*/
@TargetApi(value = Build.VERSION_CODES.M)
public void requestPermissions(Activity activity, int requestCode, PermissionCallBack permissionCallBack) {
if (mListPermissions == null) {
mListPermissions = new ArrayList();
addAllPermissions(mListPermissions);
}
if (!isOverMarshmallow()) {
return;
}
mListPermissions = findDeniedPermissions(activity, mListPermissions);
if (mListPermissions != null && mListPermissions.size() > 0) {
activity.requestPermissions(mListPermissions.toArray(new String[mListPermissions.size()]),
requestCode);
} else {
permissionCallBack.onPermissionSuccess();
}
}
public void requestResult(Activity activity, String[] permissions, int[] grantResults, PermissionCallBack permissionCallBack) {
mListPermissions = new ArrayList();
ArrayList noPermissions = new ArrayList();
ArrayList rejectPermissons = new ArrayList();
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
if (isOverMarshmallow()) {
boolean isShould = activity.shouldShowRequestPermissionRationale(permissions[i]);
mListPermissions.add(permissions[i]);
if (isShould) {
noPermissions.add(permissions[i]);
} else {
rejectPermissons.add(permissions[i]);
}
}
}
}
if (noPermissions.size() > 0) {
permissionCallBack.onPermissionFail();
} else if (rejectPermissons.size() > 0) {
ArrayList permissonsList = new ArrayList();
for (int i = 0; i < rejectPermissons.size(); i++) {
String strPermissons = rejectPermissons.get(i);
if (PERMISSIONS_STORAGE.equals(strPermissons)) {
permissonsList.add(activity.getString(R.string.permission_storage));
} else if (PERMISSIONS_ACCOUNTS.equals(strPermissons)) {
permissonsList.add(activity.getString(R.string.permission_accounts));
} else if (PERMISSIONS_PHONE.equals(strPermissons)) {
permissonsList.add(activity.getString(R.string.permission_phone));
} else if (PERMISSIONS_LOCATION.equals(strPermissons)) {
permissonsList.add(activity.getString(R.string.permission_location));
} else if (PERMISSIONS_AUDIO.equals(strPermissons)) {
permissonsList.add(activity.getString(R.string.permission_audio));
}
}
String strPermissons = permissonsList.toString();
strPermissons = strPermissons.replace("[", "");
strPermissons = strPermissons.replace("]", "");
strPermissons = strPermissons.replace(",", "、");
String strAppName = activity.getString(R.string.app_name);
String strMessage = activity.getString(R.string.permission_message);
strMessage = String.format(strMessage, strAppName, strPermissons, "\"");
permissionCallBack.onPermissionReject(strMessage);
} else {
permissionCallBack.onPermissionSuccess();
}
}
public interface PermissionCallBack {
void onPermissionSuccess();
void onPermissionReject(String strMessage);
void onPermissionFail();
}
}
/**
* Created by LH on 2016/9/4.
*/
public class PermissionActivity extends AppCompatActivity implements PermissionUtil.PermissionCallBack {
protected PermissionUtil mPermissionUtil;
private static final int PERMISSION_CODE = 999;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
mPermissionUtil = PermissionUtil.getInstance();
mPermissionUtil.requestPermissions(this, PERMISSION_CODE, this);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
mPermissionUtil.requestResult(this, permissions, grantResults, this);
}
@Override
public void onPermissionSuccess() {
initOperate();
}
@Override
public void onPermissionReject(String strMessage) {
DialogShowUtils.showPermissionAlertDialog(this, strMessage, mCancelListener, mOkListener);
}
private DialogShowUtils.PermissionDialogCancelListener mCancelListener = new DialogShowUtils.PermissionDialogCancelListener() {
@Override
public void onCancelPressed() {
finishAllActivity();
}
};
private DialogShowUtils.PermissionDialogOKListener mOkListener = new DialogShowUtils.PermissionDialogOKListener() {
@Override
public void onOKPressed() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(PermissionActivity.this)) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
};
@Override
public void onPermissionFail() {
mPermissionUtil.requestPermissions(this, PERMISSION_CODE,this);
}
}
需要注意的是工具类中权限数组要严格放入
Dangerous Permission。不能放入
Normal Permissions。
详情请参考:http://blog.csdn.net/lmj623565791/article/details/50709663