Пример #1
0
 private static void SetExitAction()
 {
     App.ExitApplication = () =>
     {
         Process.KillProcess(Process.MyPid());
     };
 }
Пример #2
0
        /**
         * 当UncaughtException发生时会转入该函数来处理
         */
        public void UncaughtException(Thread thread, Throwable ex)
        {
            if (!handleException(ex) && mDefaultHandler != null)
            {
                //如果用户没有处理则让系统默认的异常处理器来处理
                mDefaultHandler.UncaughtException(thread, ex);
            }
            else
            {
                DownloadBookService.cancel(); // 取消任务
                LogUtils.i("取消下载任务");
                new Thread(() => {
                    Looper.Prepare();
                    ToastUtils.showSingleToast("哎呀,程序发生异常啦...");
                    Looper.Loop();
                }).Start();

                try {
                    Thread.Sleep(3000);
                } catch (InterruptedException e) {
                    LogUtils.e("CrashHandler.InterruptedException--->" + e.ToString());
                }
                //退出程序
                Process.KillProcess(Process.MyPid());
                JavaSystem.Exit(1);
            }
        }
            void Run()
            {
                Process.SetThreadPriority(ThreadPriority.Background);
                if (_context._adapter == null)
                {
                    _context.SendMessage(ConstantsCustomGallery.FETCH_STARTED);
                }

                File file;
                var  selectedImages = new List <long>();

                if (_context._images != null)
                {
                    for (int i = 0, l = _context._images.Count; i < l; i++)
                    {
                        var image = _context._images[i];
                        file = new File(image.Path);
                        if (file.Exists() && image.IsSelected)
                        {
                            selectedImages.Add(image.Id);
                        }
                    }
                }

                var cursor = _context.Application.ApplicationContext.ContentResolver.Query(
                    MediaStore.Images.Media.ExternalContentUri, _context._projection,
                    MediaStore.Images.Media.InterfaceConsts.BucketDisplayName + " =?", new string[] { _context._album },
                    MediaStore.Images.Media.InterfaceConsts.DateAdded);

                if (cursor == null)
                {
                    _context.SendMessage(ConstantsCustomGallery.ERROR);
                    return;
                }

                /*
                 * In case this runnable is executed to onChange calling loadImages,
                 * using countSelected variable can result in a race condition. To avoid that,
                 * tempCountSelected keeps track of number of selected images. On handling
                 * FETCH_COMPLETED message, countSelected is assigned value of tempCountSelected.
                 */
                int tempCountSelected = 0;
                var temp = new List <Image>(cursor.Count);

                if (cursor.MoveToLast())
                {
                    do
                    {
                        if (Thread.Interrupted())
                        {
                            return;
                        }

                        var id         = cursor.GetLong(cursor.GetColumnIndex(_context._projection[0]));
                        var name       = cursor.GetString(cursor.GetColumnIndex(_context._projection[1]));
                        var path       = cursor.GetString(cursor.GetColumnIndex(_context._projection[2]));
                        var isSelected = selectedImages.Contains(id);
                        if (isSelected)
                        {
                            tempCountSelected++;
                        }

                        file = null;
                        try
                        {
                            file = new File(path);
                        }
                        catch (Exception e)
                        {
                            Log.Debug("Exception : ", e.ToString());
                        }

                        if (file.Exists())
                        {
                            temp.Add(new Image(id, name, path, isSelected));
                        }
                    } while (cursor.MoveToPrevious());
                }

                cursor.Close();

                if (_context._images == null)
                {
                    _context._images = new List <Image>();
                }

                _context._images.Clear();
                _context._images.AddRange(temp);

                _context.SendMessage(ConstantsCustomGallery.FETCH_COMPLETED, tempCountSelected);
            }
Пример #4
0
 public override void Run()
 {
     Process.SetThreadPriority(ThreadPriority.Background);
     base.Run();
 }
Пример #5
0
        /// <summary>
        /// Executes the download in a separate thread
        /// </summary>
        internal void Run()
        {
            Process.SetThreadPriority(ThreadPriority.Background);

            var state = new State(this.downloadInfo, this.downloaderService);

            PowerManager.WakeLock wakeLock = null;
            var finalStatus = ExpansionDownloadStatus.UnknownError;

            try
            {
                var pm = this.context.GetSystemService(Context.PowerService).JavaCast <PowerManager>();
                wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, this.GetType().Name);
                wakeLock.Acquire();

                bool finished = false;
                do
                {
                    Log.Debug(Tag, "DownloadThread : initiating download for " + this.downloadInfo.FileName + " at " + this.downloadInfo.Uri);
                    var requestUri = new Uri(state.RequestUri);
                    var minute     = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;
                    var request    = new HttpWebRequest(requestUri)
                    {
                        Proxy             = WebRequest.DefaultWebProxy,
                        UserAgent         = this.UserAgent,
                        Timeout           = minute,
                        ReadWriteTimeout  = minute,
                        AllowAutoRedirect = false
                    };

                    try
                    {
                        this.ExecuteDownload(state, request);
                        finished = true;
                    }
                    catch (RetryDownloadException)
                    {
                        // fall through
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("An exception in the download thread...");
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine(ex.StackTrace);
                        throw;
                    }
                    finally
                    {
                        request.Abort();
                    }
                }while (!finished);

                Log.Debug(Tag, "DownloadThread : download completed for " + this.downloadInfo.FileName);
                Log.Debug(Tag, "DownloadThread :   at " + this.downloadInfo.Uri);

                this.FinalizeDestinationFile(state);
                finalStatus = ExpansionDownloadStatus.Success;
            }
            catch (StopRequestException error)
            {
                // remove the cause before printing, in case it contains PII
                Debug.WriteLine(
                    "LVLDL Aborting request for download {0}: {1}", this.downloadInfo.FileName, error.Message);
                Debug.WriteLine(error.StackTrace);
                finalStatus = error.FinalStatus;
            }
            catch (Exception ex)
            {
                // sometimes the socket code throws unchecked exceptions
                Debug.WriteLine("LVLDL Exception for {0}: {1}", this.downloadInfo.FileName, ex.Message);
                finalStatus = ExpansionDownloadStatus.UnknownError;
            }
            finally
            {
                if (wakeLock != null)
                {
                    wakeLock.Release();
                }

                CleanupDestination(state, finalStatus);
                this.NotifyDownloadCompleted(
                    finalStatus, state.CountRetry, state.RetryAfter, state.RedirectCount, state.GotData);
            }
        }
Пример #6
0
 /// <summary>
 /// 退出APP
 /// </summary>
 public void ExitApp()
 {
     RemoveAllActivity();
     Android.OS.Process.KillProcess(Process.MyPid());
     Xamarin.Forms.Application.Current.Quit();
 }