Android Question Code to RuntimePermissions don't works in Android 4.0

asales

Expert
Licensed User
Longtime User
My minSDK is 14 and the target is 26.

Both codes below works in Android 6 and 7, but I tested in the Android 4.0 and one works and the other don't:

This code works:
B4X:
Sub btnOpenFile_Click
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE Then
        OpenMyFile
    End If
End Sub

Sub OpenMyFile
    Msgbox2Async("Do you want to continue?", "Import file", "Yes", "", "Cancel", Null, True)
    Wait For Msgbox_Result (Result As Int)
    If Result = DialogResponse.POSITIVE Then  
        Dim dlgFileExpl As ClsExplorer
        (...)
    End Sub  
End Sub

but this don't:
B4X:
Sub btnOpenFile_Click
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait for Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        OpenMyFile
    End If
End Sub

Sub OpenMyFile
(...)
End Sub

Why?
 

asales

Expert
Licensed User
Longtime User
permissions1.jpg
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Can you upload a small project that demonstrates it?
Here!
The code in button1 works.
The code in button2 don't.
Tested in device with Android 4.0.4

(Tested in Android 4.4.2 works).
 

Attachments

  • permission_test.zip
    25.8 KB · Views: 290
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You must check the sdk version (via phone lib) as runtime permissions are only available in sdk >=23. <23 means you use the old methods and you may not call runtime permissions or the app will crash.
That's not correct. You don't need to check the version. The same code should work properly on all versions.

1. The reason that the first button works is that you are not checking the value of the Result parameter.

2. It looks like a bug in Android Support library.

I tested it with the emulator. It failed with Android 4.0.3 and worked with Android 4.1.
I think that at this point it is reasonable to set the minSdkVersion to 16 (Android 4.1) and ignore the few old devices left. See the distribution chart: https://developer.android.com/about/dashboards/

Android 4.03 + Android 2.x = 0.5% of the devices.
 
Upvote 0
Top