Android Tutorial Version safe APK installation

Discussion in 'Tutorials & Examples' started by Erel, Dec 26, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    As explained in this tutorial all store apps will soon be forced to target the latest SDK.

    This means that we can no longer rely on the OS backwards compatibility features and need to handle the behavior changes ourselves.

    Starting from Android 8 (API 26) each app needs to explicitly request a special permission to install other apps. The global "install applications from unknown sources" option was removed.
    First we check whether we already have permission:
    Code:
    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
    If not we open the relevant settings page:
    Code:
    MsgboxAsync("Please allow me to install applications.""")
    Wait For Msgbox_Result(Result As Int)
    Dim in As Intent
    in.Initialize("android.settings.MANAGE_UNKNOWN_APP_SOURCES""package:" & Application.PackageName)
    StartActivity(in)
    [​IMG]

    We will then wait for Activity_Resume and check the value of CanRequestPackageInstalls again.

    The last step is to start the installation intent.


    The following non-dangerous permission is required:
    Code:
    AddPermission(android.permission.REQUEST_INSTALL_PACKAGES)
    A full example is attached. It also checks that the "installation from unknown source" option is enabled and that the external storage is writable.

    Example updated and is now based on FileProvider class.
     

    Attached Files:

    Last edited: Oct 12, 2018
  2. Mahares

    Mahares Well Known Member Licensed User

    Can you install several apk's in succession? I tried the below code, but it installs only the first app. Thank you
    Code:
    Private Sub SendInstallIntent
    For Each ApkName As String In  File.ListFiles(File.DirAssets)
      
    If ApkName.EndsWith(".apk"Then
        
    File.copy(File.DirAssets, ApkName, Starter.SharedFolder, ApkName)
        
    Dim i As Intent
        
    If phone.SdkVersion >= 24 Then
            i.Initialize(
    "android.intent.action.INSTALL_PACKAGE", CreateFileProviderUri(Starter.SharedFolder, ApkName))
            i.Flags = 
    Bit.Or(i.Flags, 1'FLAG_GRANT_READ_URI_PERMISSION
        Else
            i.Initialize(i.ACTION_VIEW, 
    "file://" & File.Combine(Starter.SharedFolder, ApkName))
            i.SetType(
    "application/vnd.android.package-archive")
        
    End If
        
    StartActivity(i)
      
    End If
     
    Next   
    End Sub
     
  3. Peter Simpson

    Peter Simpson Expert Licensed User

    Excellent tutorial @Erel, thank you...
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You will need to wait for the previous installation to complete. You can add: Wait For Activity_Resume to this loop.
     
    aidymp likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. udg

    udg Expert Licensed User

    A couple of hitches

    In Manifest there are two identical copies of "AddApplicationText( <provider" (lines 19..40).
    New FileProvider uses <files-path name="name" path="shared" /> instead of <external-files-path name="name" path="" /> as shown in post#1.
    My understandig is that
    files-path represents files in the files/ subdirectory of an app's internal storage area, so your example makes use of sub-dir "shared" inside it
    external-files-path represents files in the root of an app's external storage area (DirDefaultExternal?).

    udg
     
    Last edited: Oct 12, 2018
  7. Erel

    Erel Administrator Staff Member Licensed User

    Only one glitch. The redundant AddApplicationText was removed.

    That's true. It is better and safer than sharing from the external storage (when possible).
     
    ocalle likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice