【问题标题】:Android Runtime Permissions with SDK使用 SDK 的 Android 运行时权限
【发布时间】:2017-08-29 01:35:40
【问题描述】:

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

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 -> 安装应用程序时会询问权限。 在 SDK 25 -> 安装时不会询问权限,但调用函数时会询问

这个结构对吗?

【问题讨论】:

    标签: android runtime-permissions


    【解决方案1】:
     @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;
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-05
      • 2016-03-25
      • 1970-01-01
      • 2017-04-17
      • 2017-10-17
      • 1970-01-01
      • 2016-09-04
      相关资源
      最近更新 更多