【问题标题】:When activity crashes, the process stays alive and I cannot kill it当活动崩溃时,进程保持活动状态,我无法杀死它
【发布时间】:2012-09-20 06:33:03
【问题描述】:

我正在开发一个有很多线程的应用程序,到目前为止它经常崩溃,每当我从 eclipse 加载新版本时,它似乎创建了一个新进程。

我似乎找不到杀死旧进程的方法,它们不会出现在正在运行的应用程序中。

我可以在进程列表(“ps”命令)中看到与我的应用相关的多个进程。

有没有办法确保应用进程在崩溃时真正死掉?

【问题讨论】:

    标签: android multithreading crash kill-process


    【解决方案1】:

    你试过这个android.os.Process.killProcess(android.os.Process.myPid());它可以杀死你的进程

    【讨论】:

      【解决方案2】:

      您可以通过以下方式终止应用的进程:

      System.exit(1);
      

      为了知道您的应用程序何时崩溃(未捕获的异常),您需要定义:

      Thread.setDefaultUncaughtExceptionHandler(new CustomUncaughtExceptionHandler());
      

      在你的主线程中。

      CustomUncaughtExceptionHandler 是:

      public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler
      {
          private UncaughtExceptionHandler    defaultUEH;
      
          public CustomUncaughtExceptionHandler()
          {
              this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
          }
      
          public void uncaughtException(Thread t, Throwable e)
          {       
              // Put system.exit here
              if (defaultUEH != null)
              {
                  defaultUEH.uncaughtException(t, e);
              }
              else
              {
                  this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
                  defaultUEH.uncaughtException(t, e);
              }
          }
      

      }

      【讨论】:

      • 这不起作用,当从 Eclipse 替换它时,应用程序会死掉,但进程仍然存在,即使我实际卸载它也是如此。也许异常在另一个线程中,你认为我应该把那个处理程序也放在其他线程中吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      相关资源
      最近更新 更多