Android Question How to let "CheckAndRequest" run before Starter service starts?

flylg999

Member
Licensed User
This is my first post,Thank you for your help.
When the app is about to run,
it will crash because there is no storage permission


in the main
B4X:
Sub Activity_Create(FirstTime As Boolean)
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("..........", True )
        Sleep(1000)
        ExitApplication
    End If
 
...........


End Sub
in the starter.bas

B4X:
Sub Service_Create   
 Dim dir As String
        dir = File.DirDefaultExternal
        If  Not (File.Exists( dir, "sd.db")  ) Then
            File.Copy(File.DirAssets, "sd.jpg",  dir, "sd.db" )
        End If
        SQL1.Initialize( dir, "sd.db", True)
End sub
 
Last edited:

Erel

Administrator
Staff member
Licensed User
Welcome to the forum!

There are several mistakes here.

1. Service_Create of the starter service is run before the permission is requested.
2. You don't need a permission at all. Watch the runtime permissions video tutorial and it will be clear.
You need to use RuntimePermissions.GetSafeDirDefaultExternal.

3. I would have used File.DirInternal instead. Save yourself crashes that will happen when the secondary storage is not available.

4. Use DBUtils. It will help you: https://www.b4x.com/android/forum/threads/b4x-dbutils-2.81280/
There is a method named CopyDBFromAssets which you can use.
 
Top