Android Question Authorization by function

By-Cod3rs

Member
Licensed User
Hello. I want to write a function and call it with whatever button I want.how should i write?

Display with photo

I cannot return true or false from the function.

How do I query with the function.

It does this only on permits.


this are my codes. thank you

Error image

Adsız.png



B4X:
Sub Perssion_control As ResumableSub ' No Return type
 
    Private rp As RuntimePermissions
    If Not(rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, havePermission As Boolean)
        If havePermission = False Then
            ' No Return Type
        End If
    End If
    ' No Return Type
    Return    True
End Sub




Sub Perssion_Ok As Boolean
 
    Private rp As RuntimePermissions
    If Not(rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, havePermission As Boolean)
        If havePermission = False Then
            Return False
        End If
    End If

    Return    True
End Sub



Sub Kaydet_Buton_Click
 
    If Perssion_Ok=True Then
        Msgbox("Permisson Ok","")
        Else
        Msgbox("Permisson no","")
    End If
 
 
End Sub
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I don´t understand the question.

Anyway. You should return FALSE if the permission is not given.

ONLY use the external storage if you got a TRUE result.

how should i write?
The relevant Tutorial is this
 
Upvote 0

By-Cod3rs

Member
Licensed User
I don´t understand the question.

Anyway. You should return FALSE if the permission is not given.

ONLY use the external storage if you got a TRUE result.


The relevant Tutorial is this


I studied this and many more.

I edited the article, can you check it again?
thanks.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub Kaydet_Buton_Click If Perssion_Ok=True Then Msgbox("Permisson Ok","") Else Msgbox("Permisson no","") End If End Sub
B4X:
Sub Kaydet_Buton_Click
Wait For(Perssion_control) Complete (Result As Boolean)
   If result  Then
       Msgbox("Permisson Ok","")
  Else
     Msgbox("Permisson no","")
   End If
End Sub

BTW: You should use msgboxasync instead of msgbox. Or use LOG
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi @By-Cod3rs ,
as Manfred suggested you should really read carefully that thread since this will be a recurring situation.
Anyway, you could simplify your code like this:
B4X:
Sub Perssion_control As ResumableSub
    Private rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, havePermission As Boolean)
    Return havePermission 
End Sub

'call it like this:
Wait For(Perssion_control) Complete (Result As Boolean)

ps: you could ask for permission in your click event. A better place could be at the start of Starter module; it all depends on how you'll use that permission.
 
Upvote 0

By-Cod3rs

Member
Licensed User
problem solved .


B4X:
Sub Permisson_State As ResumableSub
    
    Private rp As RuntimePermissions
    If Not(rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Statex As Boolean)
        Return Statex
    End If

    Return    True
End Sub






Sub Kaydet_Buton_Click
Try
        Wait For (Permisson_State) Complete (State As Boolean)
    
        If State=True Then
        Msgbox("ok","")
        Else
            Msgbox ("no","")
        End If
        
    Catch
        Log(LastException)
    End Try
    
'    Private rp As RuntimePermissions
'    If Not(rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
'        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
'        Wait For Activity_PermissionResult (Permission As String, havePermission As Boolean)
'        If havePermission = False Then
'            ' No Return Type
'        End If
'    End If
    
    
    
End Sub
 
Upvote 0

By-Cod3rs

Member
Licensed User
B4X:
Sub Kaydet_Buton_Click
Wait For(Perssion_control) Complete (Result As Boolean)
   If result  Then
       Msgbox("Permisson Ok","")
  Else
     Msgbox("Permisson no","")
   End If
End Sub

BTW: You should use msgboxasync instead of msgbox. Or use LOG

When I examined the link you made, I solved the problem. Thanks for the answer. :)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
A better place could be at the start of Starter module; it all depends on how you'll use that permission.
Due to google guideline you sould always request permission when you NEED/USE a dangerous permission.

So, if you want to access external storage then you should always check for permission just before you use the external storage (but only if you got permission).

Doing the request blanket/across board is the wrong approach.
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
I agree. My comment was based on the fact that many just need to copy/access a file once at startup so it becomes a one-time operation that could even be executed silently (after the first time). Something like: access the file and send it to a remote server.
I generally avoid the external_storage since I find it risky that the final user could make use of file explorers to access and eventually delete objects needed to the app. But obviously there are circumstances where it's needed.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
problem solved .


B4X:
Sub Permisson_State As ResumableSub
  
    Private rp As RuntimePermissions
    If Not(rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Statex As Boolean)
        Return Statex
    End If

    Return    True
End Sub

Per the runtime permissions tutorial, using the Check call is somewhat redundant. You're better off just doing a CheckAndRequest call...

- Colin.
 
Upvote 0
Top