Android Question Install app from another application

wes58

Active Member
Licensed User
Longtime User
I wanted to install an app from another application. I came across this thread https://www.b4x.com/android/forum/threads/version-safe-apk-installation.87667/.
I have tried to follow anexample in this thread, but the result from the Sub CanRequestPackageInstalls was always false, even if installation of applications from unknown sources was enabled.

I had a look at https://developer.android.com/refer...ckageManager.html#canRequestPackageInstalls(), and it turns out, that according to this page: An application must target Android O or higher and declare permission Manifest.permission.REQUEST_INSTALL_PACKAGES in order to use this API.

In the example, there is a following code:
B4X:
Private Sub CheckInstallationRequirements As ResumableSub
    If File.ExternalWritable = False Then
        MsgboxAsync("Storage card not available. Make sure that your device is not connected in USB storage mode.", "")
        Return False
    Else If phone.SdkVersion >= 26 And CanRequestPackageInstalls = False Then
        MsgboxAsync("Please allow me to install applications.", "")
        Wait For Msgbox_Result(Result As Int)
        Dim in As Intent
        in.Initialize("android.settings.MANAGE_UNKNOWN_APP_SOURCES", "package:" & Application.PackageName)
        StartActivity(in)
        Wait For Activity_Resume '<-- wait for Activity_Resume
        Dim b As Boolean = CanRequestPackageInstalls
        Log("CanRequestPackageInstalls " & b)
        Return b
    Else If CheckNonMarketAppsEnabled = False Then
        MsgboxAsync("Please enable installation of non-market applications." & CRLF & "Under Settings - Security - Unknown sources" _
             & CRLF & "Or Settings - Applications - Unknown sources", "")
        Return False
    Else
        Return True
    End If
End Sub

Private Sub CanRequestPackageInstalls As Boolean
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim PackageManager As JavaObject = ctxt.RunMethod("getPackageManager", Null)
    Return PackageManager.RunMethod("canRequestPackageInstalls", Null)
End Sub
The code is looking at phone.SdkVersion >= 26, which is OS build version (which on my phone is Android 9), not a Target SDK (which is declared in the Manifest) as per android PackageManager documentation.
And that was an error in this Sub.
I had Target SDK set to be lower than 26, and that's why CanRequestPackageInstalls always returned false.
 
Top