结合网上的加上自己的感悟写的
//工具类
public class PermissionsUtils {private Activity activity;
private final int READ_PHONE_STATE_CODE = 1;
public PermissionsUtils(Activity activity) {
this.activity = activity;
}
/**
* 请求 Manifest.permission.READ_PHONE_STATE 权限
*/
public void requestPermissionPhoneState() {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA,
Manifest.permission.GET_ACCOUNTS,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_SMS
}, READ_PHONE_STATE_CODE);
}
}
/**
* 回调动作
* @param requestCode
* @param grantResults
*/
public void doNext(int requestCode, int[] grantResults) {
switch (requestCode) {
case READ_PHONE_STATE_CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
}
else {
// Permission Denied
Toast.makeText(activity, "权限被拒绝,游戏不能运行!请允许权限", Toast.LENGTH_SHORT).show();
activity.finish();
}
break;
}
}
引用:
private PermissionsUtils permissionUtils;
在onCreate中调用
permissionUtils = new PermissionsUtils(this);
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
permissionUtils.doNext(requestCode, grantResults);
xxxxx(可以写获取成功后 执行的逻辑)
}