José Teixeira
José Teixeira

Reputation: 31

How to make Xamarin Forms App Auto-Updated?

I'm developing a xamarin forms app and it needs to be auto updated without using apps market solutions (Play Store, App Center, ...).

I've already made lots of search and conceptually it should work as follwowing:

  1. The app should download an .apk file from a web service;
  2. It has to be created some kind of service to remove current app version and install the downloaded one.

My WebAPI works perfectly when i make the request in a browser. I just need some C# source code to download and install the .apk file in android device.

Upvotes: 1

Views: 2509

Answers (1)

Bruno Caceiro
Bruno Caceiro

Reputation: 7179

As you said, first you download de APK, then, just start an intent to install the downloaded file. You can adapt the Storage Location and FileName

public void DownloadFile(string m_uri, string m_filePath)
{
    var webClient = new WebClient();

    webClient.DownloadFileCompleted += (s, e) =>
    {
        string error = e.Error.Message;
        //var bytes = e.Result; // get the downloaded data
        string documentsPath = Android.OS.Environment.ExternalStorageDirectory + "/download/";


        Intent promptInstall = new Intent(Intent.ActionView).SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/download/" + "FileName.apk")), "application/vnd.android.package-archive");
                promptInstall.AddFlags(ActivityFlags.NewTask);
                StartActivity(promptInstall);    
            };

        var url = new System.Uri(m_uri);

        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
        webClient.DownloadFileAsync(url, Android.OS.Environment.ExternalStorageDirectory + "/download/FileName.apk");

     }
}

Upvotes: 3

Related Questions