Android Question Automating APK installs

nibbo

Active Member
Licensed User
OK, back on this project after a bit of a break, I must confess I hate the new APK install process and I am getting close to removing ALL automatic updating from my Apps.
Rant over...

Anyone know why I would be getting
(RuntimeException) java.lang.RuntimeException: Method: canRequestPackageInstalls not found in: android.app.ApplicationPackageManager

The sub has been copied from Erels example and I have made the Manifest changes; I guess I have missed something as I can download and run Erels example with no problem.

Thanks
 

Num3

Member
Licensed User
Did you enable your app to install packages, when it was requested the first time?!

In the manifest you must place:
B4X:
'Install updates
AddPermission(android.permission.REQUEST_INSTALL_PACKAGES)

On your program use this:

B4X:
       Dim Provider As FileProvider
            Dim i As Intent
            If phone.SdkVersion >= 24 Then
                i.Initialize("android.intent.action.INSTALL_PACKAGE", Provider.GetFileUri(Filename))
                i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
                StartActivity(i)
            Else
                i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(Provider.SharedFolder, Filename))
                i.SetType("application/vnd.android.package-archive")
                StartActivity(i)
            End If
 
Upvote 0

nibbo

Active Member
Licensed User
Did you enable your app to install packages, when it was requested the first time?!

Thanks Num3, I have those bits in the manifest and code.
The bit that is failing is in the getting permissions to install apps, as per Erels example I have the sub below:

B4X:
Private Sub CanRequestPackageInstalls As Boolean
    Dim bResult As Boolean
    Try
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim PackageManager As JavaObject = ctxt.RunMethod("getPackageManager", Null)
        bResult = PackageManager.RunMethod("canRequestPackageInstalls", Null)
    Catch
        Log(LastException)
        bResult = True
    End Try
    Return bResult
End Sub

But the line bResult = PackageManager.RunMethod("canRequestPackageInstalls", Null) returns the method not found error.
 
Upvote 0

Num3

Member
Licensed User
I don't use that code, and I can install without problems.
The first time the program tries to install, it pops the permission request, after I accept it will never ask again.

Check your SDK targets in the manifest, mine are:
B4X:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="28"/>
 
Upvote 0

nibbo

Active Member
Licensed User
SDK setting are the same, according to Erels example if your devices is above sdk 24 you need to establish the permissions manually.
Hence the call to CanRequestPackageInstalls to see if the app already has permission. If not then you run the following to get permissions:

B4X:
Dim in As Intent
in.Initialize("android.settings.MANAGE_UNKNOWN_APP_SOURCES", "package:" & Application.PackageName)
StartActivity(in)

If I don't do the bit above the app install fails so I think I need it for the device I am using.

That said.... a post in Erel's tutorial (APKSafeInstallations) checks for SDK > 24 but the code sample only does it for version > 26.

Can anyone confirm if I need it for 25? If not then why does it fail install? This is the second app that I have tried to modify and both fail!
 
Upvote 0

nibbo

Active Member
Licensed User
Found a solution; after the version check just get the user to come and see you :(

B4X:
        If sLatestVersion > sVersion Then
            '    unfortunately the automated updating processes no longer work so just get the user to ask IT to install.
            Msgbox("There is a newer version of this application. Please inform the IT department.", "Notification")
            Return
        End If
 
Upvote 0
Top