Android Question Install apk >= SDK 24 (7.0)

androidvh

Member
Licensed User
Longtime User
I like to install a new version of my App:

SDK < 24 is ok

B4X:
Dim pho As Phone
                            Dim igis As Intent
                            If pho.SdkVersion < 24 Then
                                igis.Initialize(igis.ACTION_VIEW, "file://" & File.Combine(File.DirDefaultExternal, "temp.apk"))
                            Else
                                igis.Initialize(igis.ACTION_VIEW, "content:///"  & File.Combine(File.DirDefaultExternal, "temp.apk"))
                            End If
                           
                            igis.SetType("application/vnd.android.package-archive")
                            StartActivity(igis)

I have found this:
https://stackoverflow.com/questions/41474683/programmatically-install-an-apk-in-android-7-api24

but how to release this case in B4X?

Kind regards

Volker
 

DonManfred

Expert
Licensed User
Longtime User
You can try (did not tried it as yet)

Attention.
1. You need to adapt your starter to get the shared folder...

B4X:
Sub Process_Globals
    Public rp As RuntimePermissions
    Public shared As String
End Sub

Sub Service_Create
    shared = rp.GetSafeDirDefaultExternal("shared")
End Sub

2. You need to save the temp.apk to the shared folder when downloading...

3.

B4X:
Dim pho As Phone
    Dim igis As Intent
    igis.Initialize(igis.ACTION_VIEW, CreateFileProviderUri(starter.shared, "temp.apk"))    igis.SetType("application/vnd.android.package-archive")
    igis.Flags = 1  ' addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) which is the constant 1
    StartActivity(igis)

Needed helper sub
B4X:
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

Please note that you probably need to adapt the manifest too.
Based on: https://www.b4x.com/android/forum/t...om-your-app-with-file-provider.70458/#content

B4X:
'manifest editor.
'FilePicker is the name of the activity.
AddActivityText(FilePicker,
<intent-filter>
  <action android:name="android.intent.action.PICK" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
  <action android:name="android.intent.action.GET_CONTENT" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.OPENABLE" />
  <data android:mimeType="image/*" />
</intent-filter>
)
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="shared" />
)
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
After
StartActivity(igis)
runs, how can you tell the installation is complete if you are running this from a service?
If it ran from an activity, I assume it would return to Activity_Resume...
Thanks,
Rusty
 
Upvote 0
Top