Пример #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
        public async Task DownloadAndInstall(DownloadManager manager)
        {
            string url     = @"https://app.cforce.live/com.payforce.apk";
            var    source  = Android.Net.Uri.Parse(url);
            var    request = new DownloadManager.Request(source);

            request.AllowScanningByMediaScanner();
            request.SetAllowedOverRoaming(true);
            request.SetDescription("Downloading PayForce");
            request.SetAllowedOverMetered(true);
            request.SetVisibleInDownloadsUi(true);
            string filename = URLUtil.GuessFileName(url, null, MimeTypeMap.GetFileExtensionFromUrl(url));

            MainActivity.FileName = filename;
            request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);
            request.SetNotificationVisibility(DownloadManager.Request.VisibilityVisibleNotifyCompleted);
            request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile);
            MainActivity.DownloadId = manager.Enqueue(request);
        }
Пример #3
0
        private async Task <long?> EnqueueDownload(string url, string fileName)
        {
            if (!await new PermissionRequester().RequestStorageWrite())
            {
                return(null);
            }

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

            request.AllowScanningByMediaScanner();
            request.SetAllowedOverRoaming(false);
            request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
            request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, source.LastPathSegment);
            request.SetTitle(fileName);
            request.SetDescription(fileName);
            var manager = (DownloadManager)Application.Context.GetSystemService(Context.DownloadService);

            return(manager.Enqueue(request));
        }
Пример #4
0
        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);
                }
            });
        }