question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android M onRequestPermissionResult callback not being invoked in JAVA

See original GitHub issue

Just installed a plugin that has a requestPermissions check method so that the permissions can be granted for android M before attempting to use the plugin.

When using tabris the JAVA onRequestPermissionResult callback is never invoked, and in turn the context.success is never invoked.

See offical docs for explanation https://developer.android.com/reference/android/support/v4/app/ActivityCompat.OnRequestPermissionsResultCallback.html

I’ve validated the same plugin works on vanilla Cordova Android 5.1.1 without issue.

Here is what I’ve found, others had this issue, but I’m not sure where to go from here.

1 Possibly needing Android Support Library 23.3.0 or greater http://stackoverflow.com/questions/33169455/onrequestpermissionsresult-not-being-called-in-dialog-fragment

2 Possibly related to fragments http://stackoverflow.com/questions/32714787/android-m-permissions-onrequestpermissionsresult-not-being-called

3 Tried both runOnUI and getThread pool, no dice.

cordova.getActivity().runOnUiThread(new Runnable() { })
cordova.getThreadPool().execute(new Runnable() { })

Here is the reduced version of the plugin.

class MyPermissionsPlugin extends CordovaPlugin {
    private final int REQUEST_PERMISSIONS_TRACKING_NUMBER = 59888; // Random number to track the permission request
    private CallbackContext permissionsCallback;

    @Override
    public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
        if (action.equals("requestPermission")) {
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    requestPermissionAction(callbackContext);
                }
            });
            return true;
        }
        return false;
    }

    public void requestPermissionAction(CallbackContext callbackContext) {
        String[] permissions = {
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.RECEIVE_SMS,
                Manifest.permission.READ_SMS,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.BLUETOOTH
        };

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            callbackContext.error("This only applies to android M");
            return;
        }

        permissionsCallback = callbackContext;
        cordova.requestPermissions(this, REQUEST_PERMISSIONS_TRACKING_NUMBER, permissions);
    }

    // NOTE : this method never gets invoked
    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
        // NEVER MAKES IT HERE
        if (permissionsCallback == null) {
            return;
        }
            boolean hasPermission = cordova.hasPermission(Manifest.permission.READ_PHONE_STATE);

        if (hasPermission) permissionsCallback.success("You have permission");
        else permissionsCallback.error("You've been denied by the user");
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cookiegurucommented, Jun 22, 2016

@exocom Select “Nightly” for Tabris.js version in build settings.

There shouldn’t be any changes on the JS side, but if you want the bleeding edge there too just update your package.json dependency for tabris to https://tabrisjs.com/downloads/nightly/tabris.tgz.

0reactions
mpostcommented, Jun 23, 2016

@cookieguru and thanks for explaining the update path

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android M Permissions: onRequestPermissionsResult() not ...
I ran into the same issue and I just found the solution. When using the Support library, you have to use the correct...
Read more >
Android M Permissions: onRequestPermissionsResult() not ...
I need to check permission on a button press, and if it is successful, send a text message. When I grant permission to...
Read more >
Request app permissions - Android Developers
Multiple APK support ... Overview · Java threads · Coroutines · Listenable future ... UrlRequest. Overview · Builder · Callback · Status ·...
Read more >
Nested Fragment.requestPermissions / Deny or Allow / but no ...
I need the fragment's onRequestPermissionsResult being called. ... why android given this callback in fragment if it is not coming to onPermisionResult ...
Read more >
core/core/src/main/java/androidx/core/app/ActivityCompat.java
import java.util.Arrays; ... public interface OnRequestPermissionsResultCallback { ... is invoked for every call on {@link #requestPermissions(android.app.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found