Search code examples
androidcodenameoneopentok

Codename one marshmallow authorization


I am trying to get the camera and micro permissions on Android. I added in the codenameone_library_required.properties file :

android.xpermissions=<uses-permission android:name="android.permission.CAMERA" android:required="true"/> <uses-permission android:name="android.permission.RECORD_AUDIO" android:required="true"/> <uses-permission android:name="android.permission.INTERNET" android:required="true"/>

This works for older versions of Android, but not in for Marshmallow as expected. Thus, I read on the codename one developer guide that to ask for permissions in later android version I have to target sdk 23. Indeed, I added in the build hints :

android.targetSDKVersion=23

But Unfortunately this is not sufficient and when I launch the application it does not ask for the permissions.

Should I add something to ask for permissions? Maybe the "Display.getInstance().setProperty(...)" ?

Thank you in advance!


Solution

  • If i really understood your question, you should request camera permission at runtime (android Marshmallow and above) like this:

    //to check if the user grant the permission before
    if(ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    
        //if it is not the case, request new permission
        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA},requestCode);
        return;
    }
    
    //your camera code is here 
    

    in Codename One's case, as @mina said, you couldn't find ActivityCompat class, so try this chunk of code:

    if(!com.codename1.impl.android.AndroidNativeUtil.checkForPermission(Manifest.permission.READ_PHONE_STATE, "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