0

I'm try to use runtime permission inside of a fragment.

Here is a my custom permission's class code:

public class MyPermissions {
    private String[] permissions = null;
    private Context context;
    private int permCode;
    private EventHandler handler = null;
    public MyPermissions(Context context, int permCode, String[] permissions) {

        this.context = context;
        this.permCode = permCode;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return;
        }

        List<String> perms = new ArrayList<>();

        if (permissions != null) {
            for (String perm : permissions) {
                if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED) {
                    perms.add(perm);
                }
            }
        }
        if (perms.size() > 0) {
            this.permissions = perms.toArray(new String[0]);
        }
    }

    public void doIfHasPermissions(EventHandler eventHandler) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && permissions != null) {
            if (context instanceof Activity){
                ((Activity) context).requestPermissions(permissions, permCode);
            }
            this.handler = eventHandler;
        } else {
            eventHandler.handle();
        }
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){

        if (requestCode == permCode){
            boolean success = true;
            for (int grantResult : grantResults){
                if (grantResult != PackageManager.PERMISSION_GRANTED){
                    success = false;
                }
            }

            if (success && handler != null){
                handler.handle();
            }
        }
    }
    public interface EventHandler {
         void handle();
    }
}

I'm calling this class like this:

 public void checkPermissions() {
    String[] permissions = new String[]{
            Manifest.permission.CALL_PHONE};
    myPermissions = new MyPermissions(getActivity(),
            PHONE_PERM_CODE, permissions);
    MyPermissions.EventHandler permHandler = new MyPermissions.EventHandler() {
        @Override
        public void handle() {

            makeCall();
        }
    };

    myPermissions.doIfHasPermissions(permHandler);
}

void makeCall() {
    try {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel: + 193 194"));
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This MyPermissions class is working correctly when put inside Activity, but in a fragment it does not work the first time. The second time everything is correct.

Can anyone tell me how I can rewrite my custom permission class? Thanks

Banana
  • 2,435
  • 7
  • 34
  • 60
BekaKK
  • 2,173
  • 6
  • 42
  • 80

1 Answers1

0

You will have to make changes in custom class for MyPermission class as when in android.support.v4.app.Fragment, you should use simply requestPermissions (this is an instance method of android.support.v4.app.Fragment)

Example::::

If you are in an Activity, then call

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

and if in a Fragment, just call

requestPermissions(new String[]{Manifest.permission.CAMERA},
                        MY_PERMISSIONS_REQUEST_CAMERA);

Updated::::

Please see if running Android Marshmallow 6.0, API 23

Android "onRequestPermissionsResult" not being called

krishank Tripathi
  • 616
  • 1
  • 7
  • 23