Android Question Android 6 CheckAndRequest always return True

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi guys,
I'm facing a weird problem while trying to understand Android's Runtimepermission.
I've followed the Erel's tutorial here:
https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/

It seems that does not matter what is the setting give to a resource (the Camera in this case), the CheckAndRequest methods always return TRUE!
I've done this snippet of code:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private rp As RuntimePermissions
End Sub


Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("company_layout")
  
    rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    wait for Activity_PermissionResult (permission As String, Result As Boolean)
    If Result = True Then
        Log("Camera is permitted")     
  
    Else
        Log("No Camera Permission")
    End If 

End Sub

I'm testing it on a real device with Android 6.
In the APP security menu, if I set ASK FOR ACCESS to the Camera, then as soon I get to the CheckAndRequest I see a popup dialog from the OS, asking me what to do.
Then, any button I press (DISALLOW or GRANT) the return from CheckAndRequest is always true.
If I choose, from the above dialog the option DISALLOW, then the next time I try to run it, the dialog for the choice is not even shown, and the application hangs/close.

This is my manifest file:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
AddManifestText(<uses-feature android:name="android.hardware.telephony" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="true" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="false" />)
AddManifestText(<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />)

'AddPermission("android.permission.ACCESS_COARSE_LOCATION")
AddPermission("android.permission.INTERNET")
AddPermission("android.permission.ACCESS_FINE_LOCATION")
AddPermission("android.permission.WAKE_LOCK")
AddPermission("android.permission.DEVICE_POWER")
'AddPermission("android.permission.ACCESS_COARSE_UPDATES")
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission("android.permission.VIBRATE")
AddPermission("android.permission.CAMERA")
AddPermission("android.permission.FLASHLIGHT")
AddPermission("android.hardware.camera")
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetActivityAttribute(client_activity, android:windowSoftInputMode, adjustResize|stateHidden)
'End of default text.

'********* TabStrip ***********************
CreateResource(drawable, background_tab.xml,
<selector xmlns:android="http://schemas.android.com/apk/res/android"
   android:exitFadeDuration="@android:integer/config_shortAnimTime">
  <item android:state_pressed="true" android:drawable="@color/background_tab_pressed" />
  <item android:state_focused="true" android:drawable="@color/background_tab_pressed"/>
  <item android:drawable="@android:color/transparent"/>
</selector>)
CreateResource(values, colors.xml,
<resources>
  <color name="background_tab_pressed">#6633B5E5</color>
</resources>)
'******************************************

SetActivityAttribute(dati_activity, android:windowSoftInputMode, adjustResize|stateHidden)

Anybody knows why this happen?
I'm using B4A 8.0, Real Device Android 6. Attached is also a screenshot from the SDK manager, if this make a difference.

Thank you.
 

Attachments

  • B4A SDK Manager.PNG
    B4A SDK Manager.PNG
    77.1 KB · Views: 307
Last edited:

Mike1970

Well-Known Member
Licensed User
Longtime User
You need to check the logs. If the process crashes then there should be an error message there.
Yes, there is a Crash report Log, but that log, actually make sense, since actually there was NO PERMISSION granted to the camera.

B4X:
java.lang.RuntimeException: Camera is being used after Camera.release() was called
    at android.hardware.Camera.setPreviewSurface(Native Method)
    at android.hardware.Camera.setPreviewDisplay(Camera.java:796)
    at com.google.zxing.client.android.camera.CameraManager.openDriver(CameraManager.java:110)
    at com.dlazaro66.qrcodereaderview.QRCodeReaderView.surfaceCreated(QRCodeReaderView.java:232)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:654)
    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:198)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2450)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1342)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6824)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:898)
    at android.view.Choreographer.doCallbacks(Choreographer.java:699)
    at android.view.Choreographer.doFrame(Choreographer.java:634)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:884)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:6020)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:936)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:797)

The problem is exactly that: calling rp.CheckAndRequest(rp.PERMISSION_CAMERA)
produce ALWAYS the same result: TRUE, even if a prompt from the OS is displayed and the user choose not to give the permission.
Since rp.CheckAndRequest give always true as result, how can be possible from the app code to avoid the use of the camera?
Please note that the above is the camera case, but any permission requestes give the same identical result: true everytime for any permission.
Probably is some error in the manifest, but I really do not see where it can be.
I'm using B4A 8.00. There are news about permissions in B4A 8.30? If so, I'll switch to 8.30 then.

Thank you all.
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
It should work properly with v8.00.

I've just updated Camera 1 example to work with targetSdkVersion 26: https://www.b4x.com/android/forum/t...xtends-the-camera-library-functionality.23801

Please try it.
Thank you Erel for your time. I've downloaded the new example.
I've fixed the line 68 of the Main (just using File.DirRootExternal).
I've set the real device to ASK for permission. (The options in the setting panel of the phone was: "Grant", "Deny", "Ask".)
I've then launched your sample and a popup window came from the OS asking what to do: grant access to the camera or deny it.
I've denied it, and application crashed immediately (please see the attached screenshot).
Now, if I try to run the application, no popup from OS and the application crash directly with the same error log.
So, the line ToastMessageShow("No permission!!!", True) is never reached in no cases.
 

Attachments

  • Permission Error.PNG
    Permission Error.PNG
    30.5 KB · Views: 286
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
You should run the example without any changes. Don't modify anything in the settings.
I recommend you to download again, change the package name and run it.

Which device are you using?
Ok, I've done what you said.
The changes in the Main line 68 was mandatory otherwise the compiler do not even compile due an error in the code. At line 68 was:
B4X:
Dim dir As String = Starter.SharedFolder
But there was was no SharedFolder defined in Starter. That's why I've changed it in File.DirRooExternal.
I've changed the package name in "anywheresoftware.b4a.samples.camera2"
Run the example and it works correctly since at the installation phase, the permission to the camera was granted by default.
BUT as soon I DENY or set to ASK for the permission (and then DENY it), the application crash with the same error log posted before.

The application behave correctly only if the permission is granted. If it is denied it crash and no toastmessage displayed.
I'm using a Meizu M5C (Android 6.0)
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
and for this you need to use runtimepermissions if you set targetsdk to 26 or higher.
I know, but as you can see, I do not even reach that point, since the program crash at the camera initialization (line 33 in the Main of the Erel's example).
I could even comment out all the Camera1_PictureTaken event.
The problem remain the same...
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Nothing.... I've upgraded my B4A to 8.30 but still the same problem.
I've found, by the way, that the call to CheckAndRequest return True even before the OS show the dialog asking for the user action about the permission.
Is this a valid hint for somebody?

Thanks.
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
was the app installed already in tghee past (using an lower targetsdk?
The apps permissions are remembered in android.

Try to deinstall the app.
Install it again using runtimepermissions.
Just did it.
Disinstalled. Reinstalled. Same problem.
I've discovered another strange behaviour while asking for permissions (Activity.AddMenuItem does not add any item if you ask for some permission), but this is something for another thread.

At moment, CheckAndRequest persist with the behaviour I've described.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The changes in the Main line 68 was mandatory otherwise the compiler do not even compile due an error in the code. At line 68 was:
I've fixed the example.

What you describe sounds like a problem in the device implementation.

Activity.AddMenuItem does not add any item if you ask for some permission
This is expected as you must add menu items before the program returns from Activity_Create. Start a new thread if it is not clear.
 
Upvote 0
Top