Spanish INSTALACIÓN AUTOMÁTICA DE LA APK

Anton Solans Argemí

Active Member
Licensed User
Estoy intentando hacer la instalación de una nueva versión de la APK a través del código.
Lo que hago es bajar la APK que tengo en un FTP y guardarlo en una carpeta.
Una vez finalizada la bajada hago la Instalación Automática de la APK pero cuando salgo de la pantalla de 'Conceder permiso' para 'Instalar apps desconocidas' se me cierra la app.
Adjunto código de la APP y código que me da al DEBUGAR.


B4X:
Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then
        Log(LastException.Message)
    Else
        ftp.Close                        
        ProgressDialogHide
        Install_Automatica
    End If
End Sub

Sub Install_Automatica
    Wait For (CheckInstallationRequirements) Complete (resultIA As Boolean)
    If resultIA Then
        SendInstallIntent
    End If
End Sub

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(resultIA 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
        Return CanRequestPackageInstalls
    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

Private Sub CheckNonMarketAppsEnabled As Boolean
    If phone.SdkVersion >= 26 Then Return True
    If phone.SdkVersion < 17 Or phone.SdkVersion >= 21 Then
        Return phone.GetSettings("install_non_market_apps") = "1"
    Else
        Dim context As JavaObject
        context.InitializeContext
        Dim resolver As JavaObject = context.RunMethod("getContentResolver", Null)
        Dim global As JavaObject
        global.InitializeStatic("android.provider.Settings.Global")
        Return global.RunMethod("getString", Array(resolver, "install_non_market_apps")) = "1"
    End If
End Sub

Private Sub SendInstallIntent
    Dim ApkName As String = "2nd_app.apk"
    File.copy(File.DirAssets, ApkName, Starter.Provider.SharedFolder, ApkName)
   
    Dim i As Intent
    If phone.SdkVersion >= 24 Then
        i.Initialize("android.intent.action.INSTALL_PACKAGE", Starter.Provider.GetFileUri(ApkName))
        i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
    Else
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(Starter.Provider.SharedFolder, ApkName))
        i.SetType("application/vnd.android.package-archive")
    End If
    StartActivity(i)
end sub

B4X:
mdescarga$ResumableSub_CheckInstallationRequirementsresume (java line: 538)
anywheresoftware.b4a.B4AUncaughtException
    at anywheresoftware.b4a.Msgbox.msgbox(Msgbox.java:189)
    at anywheresoftware.b4a.keywords.Common.Msgbox2(Common.java:452)
    at anywheresoftware.b4a.keywords.Common.Msgbox(Common.java:431)
    at com.Gel.mdescarga$ResumableSub_CheckInstallationRequirements.resume(mdescarga.java:538)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.keywords.Common$1.onClick(Common.java:490)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:189)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:230)
    at android.os.Looper.loop(Looper.java:319)
    at android.app.ActivityThread.main(ActivityThread.java:8893)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
 

josejad

Expert
Licensed User
Longtime User
Has probado si te funciona esta librería? Si es así, puedes echarle un vistazo al código


saludos,
 
Top