java - Android 处理 App 的关闭

标签 java android memory-management service android-asynctask

我创建了一个应用程序,需要在后台运行操作相当长的时间(假设 10 - 15 分钟)。

我正在 AsyncTask 中运行此操作。因此,在此期间,用户会最小化屏幕并像往常一样使用手机中的其他应用程序。

当此操作开始时,我将创建一个进度对话框,然后定期更新它。

但是这是我在操作结束后有时很少收到的错误

Fatal Exception: java.lang.IllegalArgumentException
View=DecorView@1234567[ABC:] not attached to window manager PackageName

这是详细的堆栈日志

Fatal Exception: java.lang.IllegalArgumentException View=DecorView@1234567[ABC:] not attached to window manager PackageName

    at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:508)

    at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:417)

    at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:136)

    at android.app.Dialog.dismissDialog(Dialog.java:446)

    at android.app.Dialog.dismiss(Dialog.java:429)

    at android.app.Dialog.cancel(Dialog.java:1353)

    at PACKAGENAME

    at android.app.Activity.runOnUiThread(Activity.java:6078)

    at PACKAGENAME

    at PACKAGENAME

    at android.os.AsyncTask.finish(AsyncTask.java:667)

    at android.os.AsyncTask.-wrap1(AsyncTask.java)

    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)

    at android.os.Handler.dispatchMessage(Handler.java:102)

    at android.os.Looper.loop(Looper.java:154)

    at android.app.ActivityThread.main(ActivityThread.java:6823)

    at java.lang.reflect.Method.invoke(Method.java)

    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)

    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

据我所知,此错误是因为 Android 操作系统想要释放一些内存,因此我的应用程序被关闭,因为这对用户不可见。但是有什么办法可以解决这个问题吗?

任何帮助将不胜感激。

编辑:这是我正在使用的代码

 public class load extends AsyncTask<Void, Void, Void> {

        @Override
        public Void doInBackground(Void... voids) {
          for(int i=0;i<number;i++){
            PerformSomeOperation();
            UpdateTheProgress();
          }         
            return null;
        }

        @Override
        protected void onPostExecute(Void n) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mProgressDialog.cancel();
                        CreateAnotherDialog();//This dialog is created to show the user completion of the progress.
                    }
                });
        }

最佳答案

您遇到此崩溃是因为您尝试在后台更新 UI,因此您的 Activity 可能会在此时被销毁。顺便说一句,onPostExecute 已经在主线程上运行您的代码,但是当您向主循环程序发送单独的消息时,您会稍微推迟逻辑,这也可能会导致问题。此外,但主要问题 - 如果用户界面无论如何都不可见,为什么要更新 UI?

此外,由于您使用 AsyncTask 作为内部类,您可能会泄漏(尽管是暂时的)您的 Activity 对象,因为它被任务隐式引用。

来自Android Documentation :

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

因此,不要将 AsyncTasks 用于长时间运行的操作。更好的方法是使用:

  1. IntentServiceBroadcastReceiver 一起与您的 Activity/Fragment 进行通信(在 API >= 26 您应该使用 JobIntentService,因为 IntentService 可能会由于后台服务的新限制而出现异常行为。
  2. RxAndroid(或只是ExecutorService/Thread)与Architecture-Components(更具体地说是LiveData) - 这样您的任务结果可以被缓存,或者可以在配置更改后继续存在。

我个人最喜欢的是选项 2。

关于java - Android 处理 App 的关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51352309/

相关文章:

java - 测试游戏算法速度

Java Socket 在低带宽时返回 EOF

java - 使用带有多个分隔符的 String.split()

c - realloc() 是否释放旧内存(当旧内存可能是指向其他内存的指针时)?

c - Valgrind 在我的机器上产生错误的输出

java - Spring MVC @ExceptionHandler 未能捕获异常

android - 在 activity_main.xml 中看不到元素布局 android studio

java - Jodatime IllegalInstantException 异常

php - Chrome自定义标签-安卓-用户代理

ios - ARC 及其工作原理。