Reputation: 31
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:
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
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