SDK的Android运行时权限

时间:2017-08-29 01:35:40

标签: android runtime-permissions

这是调用函数时询问权限的代码。

public void sendMessage(View view) {
        if(Build.VERSION.SDK_INT <23 || checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this,"Permission has granted, very nice.",Toast.LENGTH_SHORT).show();
        }
        else{
            if(!shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){
                Toast.makeText(this,"This permission is required for this action, what a pitty.",Toast.LENGTH_SHORT).show();
            }
            else{
                requestPermissions(new String[]{Manifest.permission.CAMERA},100);
                Toast.makeText(this,"If you wanna do that, you have to give permission.",Toast.LENGTH_SHORT).show();
            }
        }
    }`

这是AndroidManifest.xml

    <uses-permission android:name="android.permission.CAMERA" />

问题是。

在那种情况下, 在SDK 21 - &gt;安装应用程序时会询问权限。 在SDK 25 - &gt;它在安装时不会询问权限,但是当函数调用

结构是否正确?

1 个答案:

答案 0 :(得分:0)

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean checkCameraPermission(final Context context) {
    int currentAPIVersion = Build.VERSION.SDK_INT;
    if (currentAPIVersion >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) {
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                alertBuilder.setCancelable(true);
                alertBuilder.setTitle("Permission necessary");
                alertBuilder.setMessage("Camera permission is necessary");
                alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
                    }
                });
                AlertDialog alert = alertBuilder.create();
                alert.show();
            } else {
                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
            }
            return false;
        } else {
            return true;
        }
    } else {
        return true;
    }
}

//检查这样的权限

if(checkCameraPermission()){
    openCamera();
}  // no need to write else part,.. it will automatically ask for permission from user.

获得许可后

   @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case Constant.MY_PERMISSIONS_REQUEST_CAMERA:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                openCamera();
            }
            break;

    }
}