在寫這邊文章的時候參考了很多其他人的博客,在此感謝各位,也是給其他的伙伴做一個總結,對于我們新手來說存在的問題還是比較多的,
一、搭建iis服務器
首先我們需要搭建一個可以提供app下載最新版本的網站,在這里直接使用iis服務器,具體搭建方法就不再介紹了,這里需要注意的是要添加mime型別,這樣子才能夠下載apk檔案,具體方法請參考:https://blog.csdn.net/wsx5edc/article/details/88867019
好了iis服務器搭建完成后,放上我們新的apk安裝包,
如何生成apk檔案包具體方法請參考:https://www.cnblogs.com/hexu6788/p/10007336.html
然后同時創建一個txt檔案里面放著我們最新版號,我這里是直接使用程式集版本號,update.txt檔案里面直接寫1.0.0.1

到這里服務端就搭建完成了,
二、撰寫app升級代碼
然后就是app檔案的代碼實作了,我是參考
https://blog.csdn.net/u011821152/article/details/112966956?spm=1001.2014.3001.5501
并根據自己的需求做了一些修改,
首先在程式啟動的時候,讀取iis服務器的update檔案,對比一下當前的程式版本和服務端的是否一致,如果不一致開始下載操作
//判斷是否有新的版本需要更新
WebClient client = new WebClient();
Stream str = client.OpenRead("http://WWW.XXX.COM/update.txt");
StreamReader reader = new StreamReader(str);
var value = reader.ReadLine();
var ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
//對比版本號
if (ver != value)
{
//顯示彈窗提示用戶有新的版本需要更新
var builder = new Android.App.AlertDialog.Builder(this);
builder.SetTitle("有新版本!").SetMessage("請更新后進行使用,若是更新失敗請聯系公司管理員!當前安裝版本為"+ ver + "最新版本為"+value).SetNegativeButton("確定", (e, s) =>
{
//版本不一致呼叫升級方法
runUpdate();
}).Show();
}
在方法體的外部定義一個 ProgressDialog欄位,代表進度條視窗
ProgressDialog pg;//進度條
public void runUpdate()
{
pg = new ProgressDialog(this);
pg.SetTitle("檔案下載中...");
pg.Max = 100;
pg.SetCancelable(false);
pg.SetProgressStyle(ProgressDialogStyle.Horizontal);
pg.Show();
WebClient client = new WebClient();
//傳輸程序中更新進度條
client.DownloadProgressChanged += client_DownloadProgressChanged;
//在異步下載完成時候發生
client.DownloadFileCompleted += client_DownloadFileCompleted;
//下載檔案
var saveFolder = Android.OS.Environment.ExternalStorageDirectory;
var file = string.Format("{0}/{1}{2}", saveFolder, this.PackageName, ".apk");//fileName + "appName" + versionName + ext;
client.DownloadFileAsync(new Uri("http://www.xxx.cn:/xxx.apk"), file);
}
//更新進度條
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
pg.Progress = e.ProgressPercentage;
}
/// <summary>
/// 下載完成時進行安裝
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.UserState == null)
{
Intent i = new Intent(Intent.ActionView);
var saveFolder = Android.OS.Environment.ExternalStorageDirectory;
var file = string.Format("{0}/{1}{2}", saveFolder, this.PackageName, ".apk");
Java.IO.File apkFile = new Java.IO.File(file);
Intent intent = new Intent(Intent.ActionView);
intent.SetFlags(ActivityFlags.NewTask);
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
Android.Net.Uri uri = FileProvider.GetUriForFile(this, PackageName + ".fileprovider", apkFile);
intent.SetDataAndType(uri, "application/vnd.android.package-archive");
}
else
{
intent.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(file)), "application/vnd.android.package-archive");
}
StartActivity(intent);
}
if (e.Error != null)
{
Toast.MakeText(this, "更新例外:" + e.Error.Message, ToastLength.Long).Show();
}
pg.Dismiss();
}
三、可能出現的例外解決方法
需要注意的是 Android 7.0到來后,為了進一步提高私有檔案的安全性,Android不再由開發者放寬私有檔案的訪問權限,之前我們一直使用"file:///"絕對路徑來傳遞檔案地址的方式,
在接收方訪問時會觸發SecurityException的例外,
因此在提供檔案給第三方應用訪問時,我們就會用到FileProvider,
具體方法參考:https://www.cnblogs.com/devin_zhou/p/8520706.html
在下載完畢后進入安裝程序中會出現應用未安裝的錯誤
具體解決方法參考:https://blog.csdn.net/daijin888888/article/details/48413847
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/290692.html
標籤:其他
