Exemple #1
0
        /// <summary>
        /// Download progress
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="builder_"></param>
        private void DownloadFileProgressChanged(object sender, DownloadProgressChangedEventArgs e, Android.Support.V4.App.NotificationCompat.Builder builder_)
        {
            // Progress
            string s = e.UserState as String;

            string[] s_ = s.Split(new char[] { '|' });

            try
            {
                if (e.ProgressPercentage < 100)
                {
                    // Notification
                    builder_.SetContentTitle("Downloading - " + e.ProgressPercentage + " %");
                    builder_.SetProgress(100, e.ProgressPercentage, false);
                }
                else
                {
                    builder_.SetContentTitle("Download completed");
                    builder_.SetProgress(100, e.ProgressPercentage, false);
                }

                NotificationManager notificationManager = (NotificationManager)ActivityContext.mActivity.GetSystemService(Context.NotificationService);
                notificationManager.Notify(Convert.ToInt32(s_[2]), builder_.Build());
            }
            catch
            {
            }

#if DEBUG
            Console.WriteLine(e.ProgressPercentage + " %");
#endif

            Log.println(e.ProgressPercentage + " %");
        }
Exemple #2
0
        //public static void ShowOkAlert(Context context,string Message)
        //{
        //    AlertDialog.Builder alert = new AlertDialog.Builder(context);
        //    alert.SetMessage(Message);
        //    alert.SetPositiveButton("Ok", (senderAlert, args) => {
        //    });
        //    Dialog dialog = alert.Create();
        //    dialog.Show();

        //}

        //Download from url------------------------------------------
        public static async void DownLoadSource(string url, Java.IO.File _appDirectory, Java.IO.File newFile, string filePath, Context context, string FileName, int notificationId)
        {
            // ShowToast(context,  context.Resources.GetString(Resource.String.downloading) + Newsrc);

            var NotificationId = notificationId;

            string localPath;

            try
            {
                System.Uri uri       = new System.Uri(url);
                WebClient  webClient = new WebClient();
                string     localFileName;
                webClient.DownloadDataAsync(uri);



                Log.Debug("tag", "File to download = " + newFile.ToString());
                MimeTypeMap mime = MimeTypeMap.Singleton;
                String      ext  = newFile.Name.Substring(newFile.Name.IndexOf(".") + 1);
                String      type = mime.GetMimeTypeFromExtension(ext);

                Intent openFile = new Intent(Intent.ActionView, Android.Net.Uri.FromFile(newFile));
                openFile.SetDataAndType(Android.Net.Uri.FromFile(newFile), type);
                openFile.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop | ActivityFlags.NewTask);


                PendingIntent p = PendingIntent.GetActivity(context, 0, openFile, 0);

                var notification = new Android.Support.V4.App.NotificationCompat.Builder(context)
                                   .SetSmallIcon(Android.Resource.Drawable.StatNotifySync)
                                   .SetContentTitle(FileName)
                                   .SetContentText(" Downloading")

                                   .SetOngoing(false);
                notification.SetProgress(0, 0, true);
                NotificationManager notificationmanager = (NotificationManager)Android.App.Application.Context.GetSystemService(Service.NotificationService);
                // Build Notification with Notification Manager

                notificationmanager.Notify((int)NotificationId, notification.Build());


                webClient.DownloadDataCompleted += (s, e) =>
                {
                    try
                    {
                        if (!_appDirectory.Exists())
                        {
                            _appDirectory.Mkdirs();
                        }
                        var bytes = e.Result; // get the downloaded data
                        {
                            notification.SetContentText(" Downloaded");
                            notification.SetContentTitle(FileName);
                            notification.SetContentIntent(p);
                            notification.SetProgress(0, 0, false);
                            notification.SetAutoCancel(true);// Notification.Flags = NotificationFlags.AutoCancel;
                            //System.IO.File.WriteAllBytes(filePath, bytes);
                            notificationmanager.Notify((int)NotificationId, notification.Build());
                            // writes to local storage
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                };
            }
            catch (System.Exception ex)
            {
            }
        }