2

I have been trying to implement Run-Time permissions. The following is the code snippet for it :

RunTimePermissionsUtil.java

package com.orgzit.android.util; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.support.v4.app.ActivityCompat; public class RuntimePermissionsUtil { private static boolean shouldAskPermission() { return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M); } private static boolean shouldAskPermission(Context context, String permission) { if (shouldAskPermission()) { int permissionGrantedResult = ActivityCompat.checkSelfPermission(context, permission); return !(permissionGrantedResult == PackageManager.PERMISSION_GRANTED); } return false; } //TODO Karan 2018-09-20 Still need to investigate on the behaviour of storing in SharedPreferences whether I have asked for the permission before or not public static void checkPermission(Context context, String permission, AskPermissionListener permissionListener) { if (shouldAskPermission(context, permission)) { if (!ActivityCompat.shouldShowRequestPermissionRationale(((Activity)context), permission)) { permissionListener.onNeedPermission(); } else { permissionListener.onPermissionDisabled(); } } else { permissionListener.onPermissionGranted(); } } public interface AskPermissionListener { void onPermissionGranted(); void onNeedPermission(); void onPermissionDisabled(); } } 

Further inside an activity, AddFileActivity, I implement this listener on a Click Event, the code for the same is :

public class AddFileActivity extends DrawerActivity { private static final int CAMERA_REQ_CODE = 1001; private static final int FILE_SELECT_REQ_CODE = 1002; private Activity activity; private RuntimePermissionsUtil.AskPermissionListener askPermissionListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); activity = this; init(); getSupportActionBar().setTitle("Add File"); askPermissionListener = new RuntimePermissionsUtil.AskPermissionListener() { @Override public void onPermissionGranted() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraFileUri = getCameraFileUri(); intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraFileUri); startActivityForResult(intent, CAMERA_REQ_CODE); } @Override public void onNeedPermission() { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, CAMERA_REQ_CODE); } @Override public void onPermissionDisabled() { Toast.makeText(activity,"App does not have the permission to Open Camera :(", Toast.LENGTH_LONG).show(); } }; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { Log.d("q","q"); } private void init() { showFileChooser(); } private void showFileChooser() { final CharSequence[] items = {"Open camera", "Select from device", "Cancel"}; AlertDialog.Builder builder = new AlertDialog.Builder(AddFileActivity.this); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { if (items[item].equals("Open camera")) { RuntimePermissionsUtil.checkPermission(activity, Manifest.permission.CAMERA, askPermissionListener); Log.d("d","d"); } else if (items[item].equals("Select from device")) { //DO SOMETHING } else if (items[item].equals("Cancel")) { dialog.dismiss(); } } }); builder.show(); } 

So, all my code executes, and it goes into the onNeedPermissions() code, wherein, it goes to

ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, CAMERA_REQ_CODE); 

But, after this, astonishingly, there is no dialog asking for the RunTime Permission for camera, Rather, the dialog closes and displays the activity again.

Can someone please help me regarding this or point out a way to overcome it

3
  • could you please check that DrawerActivity has AppCompatActivity as Parent, and if permission Camera is added to your manifest? Commented Sep 21, 2018 at 9:08
  • Hi @AnisBENNSIR, Yes! DrawerActivity does extend AppCompatActivity and my Manifest does have the CAMERA permission added. Commented Sep 21, 2018 at 9:22
  • could you please remove private Activity activity; and replace with AddFileActivity.this, also change the signature of this method public static void checkPermission(Context context, String permission, AskPermissionListener permissionListener) to checkPermission(FragmentActivity activity, String permission, AskPermissionListener permissionListener) Commented Sep 21, 2018 at 9:54

1 Answer 1

3

The reasons due to which permission request dialog doesn't appear are:

  • Either the permission is already granted.
  • Or permissions have been set not to ask again.
  • Or you haven't declared the permission in your manifest.

In all cases onRequestPermissionsResult() is called immediately without showing dialog. Check it it is being called and also check the results.

Instead of writing your own utility class, I suggest you to have a look at my library: https://github.com/nabinbhandari/Android-Permissions

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.