Android Question Update APK with Intent - Response

Sabotto

Well-Known Member
Licensed User
I'm using the nice and simple app update routine of @aeric posted here.
The heart of the routine is this sub:
B4X:
Private Sub SendInstallIntent
    Dim i As Intent
    If Phone.SdkVersion >= 24 Then
        File.Copy(File.DirInternal, mNomeFile_APK, Provider.SharedFolder, mNomeFile_APK)
        i.Initialize("android.intent.action.INSTALL_PACKAGE", Provider.GetFileUri(mNomeFile_APK))
        i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
    Else
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirInternal, mNomeFile_APK))
        i.SetType("application/vnd.android.package-archive")
    End If
    StartActivity(i)
End Sub

My question is: Is there a way to know if the user has answered "Install" or "Cancel" to the message that Android automatically generates (asking whether to install the update) when the SendInstallIntent sub finishes?

Thanks
 

aeric

Expert
Licensed User
Longtime User
As I mentioned, I am not familiar with intents.
However, I am able to check if Cancel is selected.
B4X:
Sub Class_Globals
    ' other global variables
    Private ion As Object
End Sub
B4X:
Private Sub SendInstallIntent
    Dim i As Intent
    If Phone.SdkVersion >= 24 Then
        File.Copy(File.DirInternal, FileToInstall, Provider.SharedFolder, FileToInstall)
        i.Initialize("android.intent.action.INSTALL_PACKAGE", Provider.GetFileUri(FileToInstall))
        i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
    Else
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirInternal, FileToInstall))
        i.SetType("application/vnd.android.package-archive")
    End If
    'StartActivity(i)
    StartActivityForResult(i)
End Sub
B4X:
Sub ion_Event (MethodName As String, Args() As Object) As Object
    'Args(0) = resultCode
    'Args(1) = intent
    Log("MethodName=" & MethodName)
    Dim resultCode As Int = Args(0)
    If resultCode = 0 Then
        Log("Cancel")
    Else If resultCode = -1 Then 'resultCode = RESULT_OK
        Log("OK") ' never reached?
    Else
        Log("resultCode=" & resultCode)
    End If
    Return Null
End Sub

Sub StartActivityForResult (i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub
I am not sure how to get resultCode = -1.
 
Upvote 0

Sabotto

Well-Known Member
Licensed User
Good. I'm only interested in knowing if the user accepts the update or not. Since there are only two choices, I only check the Cancel. If it's not true, it means that he chose "Install"
 
Upvote 0

geps

Member
Licensed User
Longtime User
I know, unfortunately Android doesn’t provide a direct way for your app to know if the user pressed “Install” or “Cancel” on that system install prompt. Since the installation happens outside your app via an Intent, you don’t get a callback with the user’s choice.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
B4X:
Hi there, here the code for checking if update-try canceled or not...

Private Sub SendInstallIntent As ResumableSub

    Dim i As Intent

    If Phone.SdkVersion >= 24 Then
        File.Copy(File.DirInternal, FileToInstall, Starter.Provider.SharedFolder, FileToInstall)
        i.Initialize("android.intent.action.INSTALL_PACKAGE", Starter.Provider.GetFileUri(FileToInstall))
        i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
    Else
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirInternal, FileToInstall))
        i.SetType("application/vnd.android.package-archive")
    End If
    
    Try
        StartActivityForResult(i)
        Wait For ion_Event (MethodName As String, Args() As Object)
        Return 1
    Catch
        Return -1
    End Try
    
End Sub
 
Upvote 0
Top