Android Runtime Permissions with SDK

47 views Asked by At

This is the code for asking permisson when the function is called.

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();
            }
        }
    }`

And this is the AndroidManifest.xml

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

Question is that.

In that case, at SDK 21 -> it will ask permission when installing app. at SDK 25 -> it won't ask permission when installing but when function has called

is that structure is correct?

1

There are 1 answers

0
Ankit Kumar On
 @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;
    }
}

// Check permission like this

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

After permission granted

   @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;

    }
}