115 lines
5.1 KiB
C#
115 lines
5.1 KiB
C#
using Android.App;
|
|
using Android.Content;
|
|
using Android.Widget;
|
|
using System;
|
|
|
|
namespace XXCpzs.Droid
|
|
{
|
|
public class FileDownload
|
|
{
|
|
#region --自动更新--
|
|
|
|
/// <summary>
|
|
/// 使用DownloadManager下载安装文件
|
|
/// </summary>
|
|
/// <param name="mContext"></param>
|
|
/// <param name="apkAddress"></param>
|
|
public static void DownloadUpdateFile(Context mContext, string apkAddress)
|
|
{
|
|
var apkFileName = System.IO.Path.GetFileName(apkAddress);
|
|
DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(apkAddress));
|
|
// request.SetAllowedNetworkTypes(DownloadNetwork.Wifi);//网络环境
|
|
//request.SetDestinationInExternalPublicDir("download", "update1.apk");
|
|
request.SetDestinationInExternalFilesDir(mContext, Android.OS.Environment.DirectoryDownloads, apkFileName);
|
|
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
|
|
request.SetMimeType("application/vnd.android.package-archive");
|
|
//设置通知信息
|
|
request.SetTitle(apkFileName);
|
|
request.SetDescription("软件更新");
|
|
//设置为可见和可管理
|
|
request.SetVisibleInDownloadsUi(true);
|
|
//设置为可被媒体扫描器找到
|
|
request.AllowScanningByMediaScanner();
|
|
request.SetAllowedOverRoaming(false);
|
|
|
|
//下载管理
|
|
DownloadManager downManager = (DownloadManager)mContext.GetSystemService(Context.DownloadService);
|
|
//获取此次下载的ID,返回一个唯一编号(用于识别通知来源)
|
|
var refrence = downManager.Enqueue(request);
|
|
|
|
// 注册广播监听系统的下载完成事件。
|
|
IntentFilter intentFilter = new IntentFilter(DownloadManager.ActionDownloadComplete);
|
|
var boradcastReceiver = new DownloadReceiver(refrence);
|
|
mContext.RegisterReceiver(boradcastReceiver, intentFilter);
|
|
|
|
/*备注:服务器端apk文件mime类型为application/vnd.android.package-archive
|
|
* 需要权限:android.permission.INTERNET和android.permission.WRITE_EXTERNAL_STORAGE
|
|
*/
|
|
}
|
|
|
|
/// <summary>
|
|
/// 自定义下载更新
|
|
/// </summary>
|
|
/// <param name="mContext"></param>
|
|
/// <param name="apkAddress"></param>
|
|
public void DownloadUpdateFile2(Context mContext, String apkAddress)
|
|
{
|
|
//权限检查
|
|
if (mContext.CheckSelfPermission(Android.Manifest.Permission.WriteExternalStorage) != Android.Content.PM.Permission.Granted)
|
|
{
|
|
MainActivity.Instance.RequestPermissions(new string[] { Android.Manifest.Permission.WriteExternalStorage }, 1001);
|
|
return;
|
|
}
|
|
|
|
Java.IO.FileOutputStream outputStream = null;
|
|
System.IO.Stream inputStream = null;
|
|
Java.Net.HttpURLConnection connection = null;
|
|
Java.IO.File installFile = null;
|
|
try
|
|
{
|
|
var url = new Java.Net.URL(apkAddress);
|
|
connection = (Java.Net.HttpURLConnection)url.OpenConnection();
|
|
connection.ConnectTimeout = 60 * 1000;//1分钟
|
|
connection.ReadTimeout = 60 * 10 * 1000;
|
|
connection.Connect();
|
|
if (connection.ResponseCode != Java.Net.HttpStatus.Ok)
|
|
{
|
|
var errorMessage = connection.ResponseCode.ToString() + " " + connection.ResponseMessage;
|
|
return;
|
|
}
|
|
var contentLength = connection.ContentLength;
|
|
inputStream = connection.InputStream;
|
|
var downloadFile = Android.Net.Uri.FromFile(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads));
|
|
installFile = new Java.IO.File(downloadFile.Path, System.IO.Path.GetFileName(apkAddress));
|
|
outputStream = new Java.IO.FileOutputStream(installFile, false);//需要获取存储权限
|
|
byte[] data = new byte[4096];
|
|
long total = 0;
|
|
int count;
|
|
while ((count = inputStream.Read(data, 0, data.Length)) > 0)
|
|
{
|
|
total += count;
|
|
outputStream.Write(data, 0, count);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Toast.MakeText(mContext, ex.Message, ToastLength.Long).Show();
|
|
}
|
|
|
|
if (outputStream != null)
|
|
outputStream.Close();
|
|
if (inputStream != null)
|
|
inputStream.Close();
|
|
if (connection != null)
|
|
connection.Disconnect();
|
|
|
|
if (installFile != null && installFile.Exists())
|
|
{
|
|
var installIntent = new Intent(Intent.ActionView);
|
|
installIntent.SetDataAndType(Android.Net.Uri.FromFile(installFile), "application/vnd.android.package-archive");
|
|
mContext.StartActivity(installIntent);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |