Android Question Runtime permission splash screen

marcick

Well-Known Member
Licensed User
Longtime User
I wanted to put this in code snippet section but (altought it seems to work) I'm sure there are some concept error, so better some expert do some comments before.

I was trying to implement runtime permission, but I falled in a loop because permission CheckAndRequest is not a blocking code and several part of the app requires a permission.
So I have moved all my code in Main activity to a second activity MainRP. The new Main activity only ask in sequence all the permission you need. After the user has approved everything, the real activity (MainRP) is started.
A splash screen can be added to the layout of the main activity eventually.

Attached the sample project.
Any comment is appreciated.
Not sure wich problems I'll encounter for those part of code that is better to put in the starter service for example.
 

Attachments

  • RP Example.zip
    9.9 KB · Views: 475

Mahares

Expert
Licensed User
Longtime User
I redid your code based on my very limited knowledge (which is shaky) of Runtime Permission. This is all the code needed and it worked, no other activity is needed:
B4X:
'In Starter Service:
Sub Process_Globals
    Public rp As RuntimePermissions
End Sub

B4X:
'In Main Activity:
Sub Activity_Create(FirstTime As Boolean)   
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)  '
    Wait For Activity_PermissionResult (Permission As String, PResult As Boolean)
    If PResult = False Then
        MsgboxAsync("No permission to access external storage", "")
        Return
    Else
        Log("external storage")
    End If
   
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)  '
    Wait For Activity_PermissionResult (Permission As String, PResult As Boolean)
    If PResult = False Then
        MsgboxAsync("No permission to access loc", "")
        Return
    Else
        Log("fine loc")
    End If
   
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_PHONE_STATE)  '
    Wait For Activity_PermissionResult (Permission As String, PResult As Boolean)
    If PResult = False Then
        MsgboxAsync("No permission to access phone id", "")
        Return
    Else
        Dim pid As PhoneId
        Log("phone id: " & pid)
    End If

    Activity.LoadLayout("Layout1")
End Sub
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
The problem is that "Wait For Activity_PermissionResult" is not blocking the flow, so you must be careful in what you do after
Activity.LoadLayout("Layout1"), in Activity_Resume, and other service that you run in your code.
I found difficult to modify all my code to respect the sequence of what I can do or not before all permission have been accepted.
Instead, I find easy to manage the permissions in a separate activity and only when all have been accepted run my real app, without modify it.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
is not blocking the flow

i do it like this and it works fine.

B4X:
Sub checkifDirecteryexistsandcreate As ResumableSub
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult(Permission As String,  Result As Boolean)
    If Result = False  Then
        MsgboxAsync("To continue you have to accept the Permission.","Warning")
        Return False
    End If
   
    If File.Exists(File.DirRootExternal, "mySalaryBackupsNew") = False Then
        File.MakeDir(File.DirRootExternal, "mySalaryBackupsNew")
    End If
    Return True
End Sub

now you can say something like this:

B4X:
Sub backupphoneupload_Click
    Wait For(checkifDirecteryexistsandcreate) Complete (Result As Boolean)
    If Result = False Then Return 'if user denied permission sub will end here!
       
    If File.Exists(File.DirRootExternal,"pc.zip") Then File.Delete(File.DirRootExternal,"pc.zip")
    zipmode = 0
    createFullZip(zipmode)
End Sub
 
Upvote 0

Antonio Costa

Member
Licensed User
i do it like this and it works fine.

B4X:
Sub checkifDirecteryexistsandcreate As ResumableSub
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult(Permission As String,  Result As Boolean)
    If Result = False  Then
        MsgboxAsync("To continue you have to accept the Permission.","Warning")
        Return False
    End If
  
    If File.Exists(File.DirRootExternal, "mySalaryBackupsNew") = False Then
        File.MakeDir(File.DirRootExternal, "mySalaryBackupsNew")
    End If
    Return True
End Sub

now you can say something like this:

B4X:
Sub backupphoneupload_Click
    Wait For(checkifDirecteryexistsandcreate) Complete (Result As Boolean)
    If Result = False Then Return 'if user denied permission sub will end here!
      
    If File.Exists(File.DirRootExternal,"pc.zip") Then File.Delete(File.DirRootExternal,"pc.zip")
    zipmode = 0
    createFullZip(zipmode)
End Sub



posso aggiungere direttamente questo codice ? modificando solo in File.DirRootExternal, "mathtest/Listresult.txt"
i need a sub ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This is the english part of the forum. Please write english here.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
posso aggiungere direttamente questo codice ? modificando solo in File.DirRootExternal, "mathtest/Listresult.txt"
i need a sub ?

you can add it directly like this:

B4X:
'...
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult(Permission As String,  Result As Boolean)
    If Result = False  Then
        MsgboxAsync("To continue you have to accept the Permission.","Warning")
        Return 'user denied permission so exit sub!
    End If

    'user has accepted the permission so let's do something now
    If File.Exists(File.DirRootExternal, "mySalaryBackupsNew") = False Then
        File.MakeDir(File.DirRootExternal, "mySalaryBackupsNew")
    End If
 
Upvote 0

Christian García S.

Active Member
Licensed User
Hello, I have a question with a runtime permissions.

When do you work with SQLite, the suggestion is SQL1.Initialize(File.DirDefaultExternal, "test1.db", True) -> https://www.b4x.com/android/forum/threads/sql-tutorial.6736/

But when do you put the DB in this directory, do you have to ask for -> PERMISSION_WRITE_EXTERNAL_STORAGE, and the app not use this directory like images.

Do you recommend put there or put in folder File.DirAssets, or which is your suggestion.

Thanks for your help.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If you are dealing with runtime permissions and your target SDK is 23 and higher, use the below to initialize. You will not need to ask for permission, example:
B4X:
Private rp as Runtimepermissions

Private MyFolder as string
MyFolder= rp.GetSafeDirDefaultExternal("") 
SQL1.Initialize(MyFolder, "test1.db", True)
Alternatively, you can also use File.DirInternal without requesting permission.
 
Upvote 0
Top