Android Question Struggling with permissions

tsteward

Well-Known Member
Licensed User
Longtime User
Hoping someone has the patients to help with with Permissions.
I uploaded my app with targetSdkVersion="23". You know what happened next CRASH!

Anyway I have been reading the forum and spent most of today trying to understand the Runtime Permissions tutorial but I just don't get it. I might be a bit simple......so frustrated, I bet its really simple.

Anyway can someone tell me what to put where please.
 

Attachments

  • ScreenClip.png
    ScreenClip.png
    17.2 KB · Views: 146

DonManfred

Expert
Licensed User
Longtime User
B4X:
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
if success then
  ' read from External storage is granted...
end if

Do this for ALL permissions which are marked with a * in the Permissionlist.
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
B4X:
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
if success then
  ' read from External storage is granted...
end if

Do this for ALL permissions which are marked with a * in the Permissionlist.
Sorry to waste your time but I just have mental block and NO IDEA!

I have a little mock up below can you please show how to do phone_state and external storage.
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim DBFileName As String                : DBFileName = "helper.db"
    Dim DBFileDir As String                    : DBFileDir = File.DirDefaultExternal
    Dim phid As PhoneId
    Dim rp As RuntimePermissions
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim custID As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    If FirstTime Then
        rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If success Then
            ' read from External storage is granted...
        End If
        
        custID = phid.GetDeviceId
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)

End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The best way is to call CheckAndRequest right before you need the permission. If it is not feasible then call it from Activity_Resume or Activity_Create (but not just when FirstTime is true).

This way you give the user a change to allow the permission even if they previously denied it. Nothing bad happens if the permission was already granted.

B4X:
rp.CheckAndRequest(rp.PERMISSION_READ_PHONE_STATE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then
   custID = phid.GetDeviceId
Else
 'no permission
End If
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
The best way is to call CheckAndRequest right before you need the permission. If it is not feasible then call it from Activity_Resume or Activity_Create (but not just when FirstTime is true).

This way you give the user a change to allow the permission even if they previously denied it. Nothing bad happens if the permission was already granted.

B4X:
rp.CheckAndRequest(rp.PERMISSION_READ_PHONE_STATE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then
   custID = phid.GetDeviceId
Else
 'no permission
End If
This has my app asking for permission GREAT THANKS
But if I select 'Deny' it does not enter the else command.

Tony
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
I put Erel's code right in place of asking for phoneid.
B4X:
rp.CheckAndRequest(rp.PERMISSION_READ_PHONE_STATE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then
   custID = phid.GetDeviceId
Else
   CustID = rnd.....
End If
I thought i wasn't getting into the else part of the code but i was just having a moment.

Because my brain was fried i didn't pickup where don had suggested same thing but used two different variables for the result in his code. So his answer is also correct. As you would expect.
 
Upvote 0
Top