示例#1
0
        private void BtnDownload_Click(object sender, EventArgs e)
        {
            FileDelete();

            var manager = DownloadManager.FromContext(this);

            var url = new System.Uri(string.Format(@"{0}{1}", "http://172.34.34.144:8080/barcodescanner", string.Format("{0}{1}{2}", "/", Application.Context.PackageName, ".apk")));

            //var url = new System.Uri(string.Format(@"{0}{1}", "http://172.34.34.144:8080/barcodescanner", string.Format("{0}{1}", "/", "com.daesangit.barcodescanner.apk")));

            try
            {
                var request = new DownloadManager.Request(Android.Net.Uri.Parse(url.ToString()));
                request.SetMimeType("application/vnd.android.package-archive");
                request.SetTitle("App Download");
                request.SetDescription("File Downloading...");
                request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile);
                request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
                request.SetAllowedOverMetered(true);
                request.SetAllowedOverRoaming(true);

                request.SetDestinationUri(Android.Net.Uri.FromFile(new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath + string.Format("{0}{1}{2}", "/", Application.Context.PackageName, ".apk"))));

                System.Console.WriteLine(Android.OS.Environment.DirectoryDownloads);
                System.Console.WriteLine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath);

                downloadId = manager.Enqueue(request);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }
示例#2
0
文件: Update.cs 项目: UMI64/PINS
        private bool DownLoad(string Url)
        {
            try
            {
                /*检查是否有访问存储权限*/
                if (requestAllPower() == false)
                {
                    return(false);
                }

                DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(Url));
                request.SetAllowedNetworkTypes(Android.App.DownloadNetwork.Wifi);//设定只有wifi环境下载
                request.SetTitle("我的下载");
                request.SetDescription("下载一个大文件");
                request.SetMimeType("application/vnd.android.package-archive");
                request.SetNotificationVisibility(Android.App.DownloadVisibility.Visible | Android.App.DownloadVisibility.VisibleNotifyCompleted);
                request.SetDestinationUri(Android.Net.Uri.FromFile(file));
                this.RunOnUiThread(() =>
                {
                    Toast.MakeText(this, "正在下载", ToastLength.Short).Show();
                });
                downloadId = downloadManager.Enqueue(request);
                return(true);
            }
            catch (Exception ex)
            {
                this.RunOnUiThread(() =>
                {
                    Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
                });
                return(false);
            }
        }
示例#3
0
        public void DownloadAndInstallApp(string downloadurl)
        {
            var savefilename = downloadurl.Substring(downloadurl.LastIndexOf("/", StringComparison.Ordinal) + 1);

            //  var path = Path.Combine(savedir, savefilename);


            DownloadManager.Request request =
                new DownloadManager.Request(Android.Net.Uri.Parse(downloadurl));
            request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, savefilename);
            // request.SetDestinationInExternalFilesDir(Application.Context, Android.OS.Environment.DirectoryDownloads, update. DownloadFileName);
            request.SetMimeType("application/vnd.android.package-archive");
            enqueeId = downloadManager.Enqueue(request);
        }
        /// <summary>
        /// Download an apk from a given url and name it with the given version number
        /// </summary>
        /// <param name="urlOfTarget">The url of the apk</param>
        /// <param name="targetVersion">The version number to append</param>
        public void Download(string urlOfTarget, string targetVersion)
        {
            var uri          = Android.Net.Uri.Parse(urlOfTarget);
            var request      = new DownloadManager.Request(uri);
            var downloadName = this.PackageName + "." + targetVersion + ".apk";

            request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
            request.SetAllowedOverMetered(true);
            request.SetTitle(string.Format("App update, Version: {0}", targetVersion));
            request.SetDestinationInExternalFilesDir(this.context, Android.OS.Environment.DirectoryDownloads, downloadName);
            request.SetMimeType("application/vnd.android.package-archive");

            this.downloadReference = this.downloadManager.Enqueue(request);
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.AutoUpdateLayout);

            progressBar = FindViewById <ProgressBar>(Resource.Id.progressBar1);
            textView1   = FindViewById <TextView>(Resource.Id.textView1);

            progressBar.Max = 100;
            textView1.Text  = "0 %";

            FileDelete();

            var manager = DownloadManager.FromContext(this);
            //var manager = (DownloadManager)GetSystemService(Context.DownloadService);

            var url = new System.Uri(string.Format(@"{0}{1}", GlobalSetting.Instance.MOBILEEndpoint.ToString(), string.Format("{0}{1}{2}", "/", Application.Context.PackageName, ".apk")));

            var request = new DownloadManager.Request(Android.Net.Uri.Parse(url.ToString()));

            request.SetMimeType("application/vnd.android.package-archive");
            request.SetTitle("App Download");
            request.SetDescription("File Downloading...");
            request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile);
            request.SetNotificationVisibility(DownloadVisibility.Visible);
            request.SetAllowedOverMetered(true); //모바일 네트쿼크
            request.SetAllowedOverRoaming(true); //로밍

            Java.IO.File path = this.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads);
            request.SetDestinationUri(Android.Net.Uri.FromFile(new Java.IO.File(path, string.Format("{0}{1}", Application.Context.PackageName, ".apk"))));

            downloadId = manager.Enqueue(request);

            Task.Run(() =>
            {
                for (; ;)
                {
                    var query = new DownloadManager.Query();
                    query.SetFilterById(downloadId);
                    ICursor cursor = manager.InvokeQuery(query);
                    if (cursor.MoveToFirst())
                    {
                        var soFar = cursor.GetDouble(cursor.GetColumnIndex(DownloadManager.ColumnBytesDownloadedSoFar));
                        var total = cursor.GetDouble(cursor.GetColumnIndex(DownloadManager.ColumnTotalSizeBytes));
                        RunOnUiThread(() =>
                        {
                            progressBar.SetProgress(System.Convert.ToInt32(soFar / total * 100), true);
                            textView1.Text = string.Format("{0} %", Convert.ToInt32(soFar / total * 100));
                        });
                        System.Console.WriteLine(soFar.ToString());

                        if (soFar.Equals(total))
                        {
                            break;
                        }
                    }
                    Thread.Sleep(500);
                }
            });
        }
示例#6
0
        /// <summary>
        /// 开始下载
        /// </summary>
        public void Start(string apkUrl, string apkName)
        {
            var receiver = new DownLoadCompleteReceiver();

            // 注册广播接收器,当下载完成时自动安装
            _context.RegisterReceiver(receiver, new IntentFilter(DownloadManager.ActionDownloadComplete));

            if (DownloadMode != DownloadMode.Always)
            {
                var filePath = AEnvironment.GetExternalStoragePublicDirectory(DownloadDirectory).Path;
                file = new JFile(filePath, apkName);
                if (file.Exists())
                {
                    Console.WriteLine(file.Path);
                    if (DownloadMode == DownloadMode.Overwrite)
                    {
                        file.Delete();
                        //更新下载中的显示
                        //ContentValues values = new ContentValues ();
                        //values.Put ("deleted", 1);
                        //COLUMN_DELETED = "deleted";
                        //CONTENT_URI = Uri.parse("content://downloads/my_downloads");
                        //ALL_DOWNLOADS_CONTENT_URI = Uri.parse("content://downloads/all_downloads")
                        //_context.ContentResolver.Update (ContentUris.WithAppendedId (AUri.Parse ("content://downloads/my_downloads"), _downloadID), values, null, null);
                        _context.ContentResolver.Delete(AUri.Parse("content://downloads/my_downloads"), "_data=?", new string[] { file.Path });
                    }
                    else
                    {
                        //发送广播
                        var intent = new Intent();
                        intent.SetAction(DownloadManager.ActionDownloadComplete);
                        _context.SendBroadcast(intent);
                        return;
                    }
                }
            }

            //判断是否有正在下载的相同任务
            DownloadManager.Query downloadQuery = new DownloadManager.Query();
            //根据下载状态查询下载任务
            downloadQuery.SetFilterByStatus(DownloadStatus.Running);
            //根据任务编号查询下载任务信息
            //downloadQuery.setFilterById(id);
            var cursor = downloadManager.InvokeQuery(downloadQuery);

            if (cursor != null)
            {
                if (cursor.MoveToNext())
                {
                    if (apkUrl.Equals(cursor.GetString(cursor.GetColumnIndex(DownloadManager.ColumnUri))))
                    {
                        Toast.MakeText(_context, "已存在的下载任务", ToastLength.Short).Show();
                        cursor.Close();
                        return;
                    }
                }
                cursor.Close();
            }

            downLoadRequest = new DownloadManager.Request(AUri.Parse(apkUrl));
            downLoadRequest.SetTitle(Title);
            downLoadRequest.SetDescription(Description);
            downLoadRequest.AllowScanningByMediaScanner();
            downLoadRequest.SetVisibleInDownloadsUi(true);
            //设置在什么网络情况下进行下载
            downLoadRequest.SetAllowedNetworkTypes(AllowedNetworkType);
            //设置通知栏标题
            downLoadRequest.SetNotificationVisibility(DownloadVisibility.Visible | DownloadVisibility.VisibleNotifyCompleted);
            //设置下载文件的mineType
            downLoadRequest.SetMimeType("application/com.trinea.download.file");
            //downLoadRequest.SetDestinationInExternalFilesDir
            //设置保存位置
            downLoadRequest.SetDestinationInExternalPublicDir(DownloadDirectory, apkName);
            //保存任务ID
            _downloadID = downloadManager.Enqueue(downLoadRequest);
        }