tip1
判定是否有权限:checkSelfPermission()
tip2
如果没有权限,弹出dialog给用户选择:requestPermission(),第二个参数code与onRequestPermissionResult()方法中的code对应
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
REQUEST_CODE_ASK_PERMISSON);
}
tip3
判断用户是否确认了权限onRequestPermissionResult ()
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSON:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
} else {
// Permission Denied
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
tip4
在弹出权限选择的对话框前给用户show一个dialog,用于引导用户进行选择。
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Explain to the user why we need to read the contacts
showMessage("请允许应用对SD卡进行读写操作",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
requestPermissions(
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
REQUEST_CODE_ASK_PERMISSIONS);
}
});
return;
}
requestPermissions(
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
REQUEST_CODE_ASK_PERMISSIONS);
}
private void showMessage(String message,
DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MainActivity.this).setMessage(message)
.setPositiveButton("OK", okListener).create().show();
}
多权限循环授权
private String[] permissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,};
private List<String> permissionList = new ArrayList<>();
private void addPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(permissions[0]);
}
if (checkSelfPermission(permissions[1]) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(permissions[1]);
}
if (!permissionList.isEmpty()) {
String[] permission = permissionList.toArray(new String[permissionList.size()]);
requestPermissions(permission, 1);
}
}
}
Service等无界面应用动态权限授予
public class PermissionActivity extends Activity {
private static final String TAG = "PermissionActivity";
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_permission);
mContext = this;
requestPermissions(
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
Log.i(TAG, "request permisson");
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Log.i(TAG, "get permisson");
Log.i(TAG, "finish PermissionActivity");
Constants.PERMISSION = false;
finish();
} else {
// Permission Denied
Log.i(TAG, "refuse permisson");
Log.i(TAG, "finish PermissionActivity");
finish();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
}
}