Android Question App crash during the "request access permission"

Paolo Pini

Member
Licensed User
Longtime User
Hi ,
when my app start Android open the 'request permission access confirm box' and wait that I press OK to open the next form with the permission selection list.
The problem is that my app still running in background while this box is open and if I don't press OK quickly it crash because timeout.


Immagine.jpg


My manifest.

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-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="3"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

AddPermission(android.permission.MANAGE_EXTERNAL_STORAGE)
SetApplicationAttribute(android:requestLegacyExternalStorage, true)

Code I found to enable the access file permission.

B4X:
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
    
    If FirstTime Then
        MES.Initialize(Me, "MES")
    End If
    
    ' get the device SDK version
    Dim SdkVersion As Int = device.SdkVersion
    
    ' Choose which permission to request in order to access external storgage
    If SdkVersion < 30 Then
        Log("SDK = " & SdkVersion & " : Requesting WRITE_EXTERNAL_STORAGE permission")
        Dim rp As RuntimePermissions
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
        Wait For Activity_PermissionResult (permission As String, Result As Boolean)
        Log($"PERMISSION_WRITE_EXTERNAL_STORAGE = ${Result}"$)
    Else
        Log("SDK = " & SdkVersion & " : Requesting MANAGE_EXTERNAL_STORAGE permission")
        Log("On Entry MANAGE_EXTERNAL_STORAGE = " & MES.HasPermission)
        
        If Not(MES.HasPermission) Then
            MsgboxAsync("This app requires access to all files, please enable the option", "Manage All Files")
            Wait For Msgbox_Result(Res As Int)
            Log("Getting permission")
            MES.GetPermission
            Wait For MES_StorageAvailable
            Log("Completato")
        End If
    End If
End Sub

Thanks in advance

Paolo
 

Sagenut

Expert
Licensed User
Longtime User
Your project is B4XPages.
Do not write code in the Main, you need to do it in the B4XMainPage.
The code for Runtime Permissions in B4XPages is
B4X:
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
 
Upvote 1

Paolo Pini

Member
Licensed User
Longtime User
Your project is B4XPages.
Do not write code in the Main, you need to do it in the B4XMainPage.
The code for Runtime Permissions in B4XPages is
B4X:
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
Thank you for your quick reply,

You mean the code:

B4X:
    Dim MES As ManageExternalStorage
    Dim pm As B4XPagesManager

    pm.Initialize(Activity)

    If FirstTime Then
        MES.Initialize(Me, "MES")
    End If
..........

is useless?

Thanks

Paolo
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I mean that you must put it in the B4XMainPage that is the real start of your app, in
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
Even better if you create a separate Sub to ask for permissions and call it at the end of B4XPage_Created.
And you need to modify all this lines
B4X:
Wait For Activity_PermissionResult (permission As String, Result As Boolean)
into this
B4X:
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
 
Upvote 0
Top