Fork us on GitHub

Marshmallow Permissions in the Simulator and Native Code

Simplifying and empowering the new native Android permission system
Post Image

Marshmallow Permissions in the Simulator and Native Code

We talked about the new Android 6 (Marshmallow) permissions in Codename One last week and so far we’ve been pretty happy with the result. We had some build regressions on the older Ant based build path but those were fixed shortly after and it’s been smooth sailing since then. As part of the transition to the new permissions system we added two features to the simulator and the AndroidNativeUtil class.

Simulate Permission Prompts

You can simulate permission prompts by checking that option in the simulator menu.

Simulate permission prompts menu item in the simulator
Figure 1. Simulate permission prompts menu item in the simulator

This will produce a dialog to the user whenever this happens in Android and will try to act in a similar way to the device. Notice that you can test it in the iOS simulator too.

AndroidNativeUtil’s checkForPermission

If you write Android native code using our native interfaces you are probably familiar with the AndroidNativeUtil class from the package.

This class provides access to many low level capabilities you would need as a developer writing native code. Since native code might need to request a permission we introduced the same underlying logic we used namely: checkForPermission.

To get a permission you can use this code as such:

if(!, "This should be the description shown to the user...")){
    // you didn't get the permission, you might want to return here
// you have the permission, do what you need

This will prompt the user with the native UI and later on with our fallback option as described in the previous blog post. Notice that the checkForPermission method is a blocking method and it will return when there is a final conclusion on the subject. It uses invokeAndBlock and can be safely invoked on the event dispatch thread without concern.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.