Android Question Permissions in secondary activity

udg

Expert
Licensed User
Longtime User
Hi all,

are Permissions allowed to be requested in a secondary Activity? I guess so, but..

yesterday I was playing with the excellent lib QRCodeReaderView by @Johan Schoeman and tried to ask for the Camera permission in a secondary (i.e. not Main) activity. It failed, crashing the app.
Moving the CheckAndRequest to Main solved the problem.

So my question is: can we request Permissions in a secondary activity?
If yes, could my problem be due to:
1. my phone (Android 7, Manifest set to targetsdk = 26)
2. the lib
3. something in my coding

TIA
 
Last edited:
D

Deleted member 103

Guest
are Permissions allowed to be requested in a secondary Activity? I guess so, but..
I ask the Permissions in different Activity, no problem.
It is only important that the permissions in the starter service is initialized.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Thank you, Filippo. That leads us to one of the three alternatives I listed (or any other you can think of).
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Start with posting the complete errormessage and the relevant code
It should work in another Activity
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I've to recreat it (i.e. moving Permission to secondary activity and generate the error). Be back in a few minutes.

'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"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
AddPermission("android.permission.VIBRATE")
AddPermission("android.permission.CAMERA")
AddPermission("android.permission.FLASHLIGHT")

B4X:
Sub btnStart_Click
   Dim Result As Boolean = True
   'Johan variant                                                                      'ADDED 8 June 2018
   'If Not(Starter.rp.Check(Starter.rp.PERMISSION_CAMERA)) Then                                                        'ADDED 8 June 2018
   '   Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)                                                       'ADDED 8 June 2018
   '   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)                   'ADDED 8 June 2018
   'End If
   Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result Then
       qrcrv.Enabled = True
       Sleep(0)
       qrcrv.startScan
   Else
       ToastMessageShow("Negato accesso alla fotocamera", True)
   End If
End Sub

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
(MyMap) {usr_id=2, usr_name=XYZ, usr_stat=1, usr_aznd=2, usr_pet=1, err=0}
** Activity (main) Pause, UserClosed = false **
** Activity (actmanagec) Create, isFirst = true **
0
1
0
1
** Activity (actmanagec) Resume **
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:643)
at android.hardware.Camera.open(Camera.java:491)
at com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface.open(GingerbreadOpenCameraInterface.java:58)
at com.google.zxing.client.android.camera.open.CameraManager.openDriver(CameraManager.java:82)
at com.dlazaro66.qrcodereaderview.QRCodeReaderView.surfaceCreated(QRCodeReaderView.java:170)
at android.view.SurfaceView.updateWindow(SurfaceView.java:709)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:180)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2560)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1469)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7007)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
at android.view.Choreographer.doCallbacks(Choreographer.java:709)
at android.view.Choreographer.doFrame(Choreographer.java:644)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

As soon as the code enters the secondary activity (actManageC) it crashes. It's like it needs the permission given well before the user could click on the start button.
The 0-1-0-1 logs are from the Activity_Create as per Johan's code.

What I see is that both Create and Resume work ok, then the crash happens.

Moving the permission request to Main (so before launching the secondary activity) solves the problem. On first run a dialog request is shown, on subsequent request the code proceeds straight to the point. No problems at all.
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
I did and it worked. That is the exact code from Johan moved to Activity2, right?

Maybe part of the problem resides in me enablig/disabling the qrcrv instead of showing/hiding.
In my layout it resides in panel which is a child of a 100%x-100%y backpanel.

Strange thing is that just moving those two rp lines to Main everything works as expecxted.
 
Upvote 0
Top