Share via


How to install Apk from app using Xamarin android

Question

Thursday, January 23, 2020 3:41 PM

I am trying to update my app programmatically.

I am able to download the new version of the app apk.

But when the installation code runs nothing happens except an activity that is opened and then closed returning to my app

The installation code is

var pathToNewFolder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/got/download";

Java.IO.File apkFile = new Java.IO.File(pathToDownloadFolder + "com.trivalor.T_GO.apk");

Intent promptInstall = new Intent(Intent.ActionView); promptInstall.AddFlags(ActivityFlags.NewTask); promptInstall.AddFlags(ActivityFlags.GrantReadUriPermission);

if (CrossDeviceInfo.Current.VersionNumber.Major < 7) promptInstall.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(pathToDownloadFolder + "com.tri.GOT.apk")), "application/vnd.android.package-archive"); else promptInstall.SetDataAndType(FileProvider.GetUriForFile(CrossCurrentActivity.Current.Activity, "com.tri.GOT.fileprovider", apkFile), "application/vnd.android.package-archive"); CrossCurrentActivity.Current.Activity.StartActivity(promptInstall);

All replies (3)

Friday, January 24, 2020 2:07 AM

try to use this :

    public static bool install(Context con, string filePath)
    {
        try
        {
            if (TextUtils.IsEmpty(filePath))
                return false;
            File file = new File(filePath);
            if (file.Exists())
            {
                return false;
            }
            Intent intent = new Intent(Intent.ActionView);
            intent.SetFlags(ActivityFlags.NewTask);
            if (Build.VERSION.SdkInt >= Build.VERSION_CODES.N)
            {
                intent.AddFlags(ActivityFlags.GrantReadUriPermission);//add read and write permissions
            }
            intent.SetDataAndType(GetPathUri(con, filePath), "application/vnd.android.package-archive");
            con.StartActivity(intent);
        }
        catch (Exception e)
        {
            e.PrintStackTrace();
            Toast.MakeText(con, "Installation failed. Please download again", ToastLength.Short).Show();
            return false;
        }
        catch (Error error)
        {
            error.PrintStackTrace();
            Toast.MakeText(con, "Installation failed. Please download again", ToastLength.Short).Show();
            return false;
        }
        return true;
    }


    public static Android.Net.Uri GetPathUri(Context context, string filePath)
    {
        Android.Net.Uri uri;
        if (Build.VERSION.SdkInt >= Build.VERSION_CODES.N)
        {
            string packageName = context.PackageName;
            uri = FileProvider.GetUriForFile(context, packageName + ".fileProvider", new File(filePath));
        }
        else
        {
            uri = Android.Net.Uri.FromFile(new File(filePath));
        }
        return uri;
    }

Add providers and permissions to the AndroidManifest

     <application
  ...>
      <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>
</application>

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

Create the XML folder under res, and then proselyte the file_paths.xml file in the XML folder

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <root-path name="root" path="" />
    <external-path name="external_storage_root" path="." />
    <external-path name="external_storage_download" path="Download" />
</paths>

Friday, January 24, 2020 8:58 AM

@LeonLu I will give it a try and let you know if it works


Thursday, May 14, 2020 7:24 AM

Encontró una solución? llevo días buscando lo mismo :(