Android Question A great proposition...

bskotu555

Active Member
I have saved the APP file in binary format to the database. When the APP starts, I can query the binary data of this APP by its name. After downloading it to the variable (Dim ap() As Byte), how can I install the APP?
 
Solution
B4A:
If bb<>vs Then'开启升级模式
        If ap<>Null Then
            File.WriteBytes(File.DirInternal,"1.apk",ap)
            '自动安装
           
        End If
    End If
I don't know how to write code next。。。
B4X:
File.WriteBytes(File.DirInternal,"1.apk",ap)
'自动安装
SendInstallIntent

Class_Globals
B4X:
Private FileToInstall As String = "1.apk"

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...

bskotu555

Active Member
Write it as an APK file.
B4a:
Sub update
    Dim Cursor As JdbcResultSet
    Dim vs As String ="V1.1"
    Dim nm As String ="SMT进料图采集器"
    StartService(Starter)
    Sleep(50)
    Cursor = Starter.Mysql_Jdbc.ExecQuery2("SELECT * FROM app库 WHERE `名称`=? ", Array As String(nm)) '防止SQL注入
    Do While Cursor.NextRow
        Dim bb As String =Cursor.GetString("版本")
        Dim ap() As Byte =Cursor.GetBlob("文件")
    Loop
    If bb<>vs Then'开启升级模式
        If ap<>Null Then
            
            
        End If
    End If
End Sub
I don't know how to write code next。。。
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I think this code snippet (taken from B4A-Bridge source) could help.
B4X:
Private Sub SendInstallIntent
    Dim i As Intent
    If Phone.SdkVersion >= 24 Then
        i.Initialize("android.intent.action.INSTALL_PACKAGE", CreateFileProviderUri(Starter.folder, "temp" & apkName & ".apk"))
        i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
    Else
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(Starter.folder, "temp" & apkName & ".apk"))
        i.SetType("application/vnd.android.package-archive")
    End If
    StartActivity(i)
End Sub

Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub

Manifest Editor:
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <external-files-path name="name" path="" />
)
AddPermission(android.permission.REQUEST_INSTALL_PACKAGES)
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
To demonstrate the code above, I just created an example for updating the app.
 
Upvote 0

bskotu555

Active Member
To demonstrate the code above, I just created an example for updating the app.
B4A:
If bb<>vs Then'开启升级模式
        If ap<>Null Then
            File.WriteBytes(File.DirInternal,"1.apk",ap)
            '自动安装
            
        End If
    End If
I don't know how to write code next。。。
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4A:
If bb<>vs Then'开启升级模式
        If ap<>Null Then
            File.WriteBytes(File.DirInternal,"1.apk",ap)
            '自动安装
           
        End If
    End If
I don't know how to write code next。。。
B4X:
File.WriteBytes(File.DirInternal,"1.apk",ap)
'自动安装
SendInstallIntent

Class_Globals
B4X:
Private FileToInstall As String = "1.apk"

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)
End Sub
 
Upvote 0
Solution

aeric

Expert
Licensed User
Longtime User
B4A:
If bb<>vs Then'开启升级模式
        If ap<>Null Then
            File.WriteBytes(File.DirInternal,"1.apk",ap)
            '自动安装
           
        End If
    End If
I don't know how to write code next。。。
Can you close this question and mark my answer as solution if it solved your problem?
 
Upvote 0
Top