android - 干净地退出android应用程序

标签 android exit

我在任何地方寻找这个主题都说(恢复):“在您的 Activity 上调用 finish(),如果您有任何服务正在运行,请调用 stopService()在他们身上”。

所以我这样做了,而且看起来很有效,我不再担心了。今天,调试我的一部分代码,我需要将 Log 行添加到一个非结束线程的方法中,我想在应用程序的整个生命周期中执行它,基本上是这样的:

final Thread buf_liberator = new Thread(
  new Runnable() {
    public void run() {
      while (true) {
        methodCall();
        SystemClock.sleep(9000);
      }
    }
  } 
);
buf_liberator.setPriority(7);
buf_liberator.start();

methodCall() 中,我放置了 Log 行,令我惊讶的是,在我关闭我的应用程序后按下调用 finish()< 的退出按钮 并停止了我已经启动的服务,我仍然在 LogCat 中无限期地每 9 秒看到一次 Log 消息。

所以问题是(是):这个线程不是一个服务,为什么它一直运行?难道我做错了什么?这是预期的行为吗?是否有真正的方法来销毁应用内存中的所有内容?

谢谢!

最佳答案

So the question(s) is(are): This thread is not a service, why it keeps running?

因为你的进程还在运行。即使当您 finish() 一个 Activity 或 stop() 一个 Service 时,您的应用程序进程可能会继续运行。

Am I doing something wrong?

除了创建一个永无止境的线程?没有。

Is this an expected behavior?

是的。 Android 系统将进程作为缓存机制保留,以加速恢复以前使用过的应用程序。系统最终会在它认为必要时终止你的进程(和正在运行的线程)。 不要指望你的线程不会结束,因为它最终会结束

Is there a real method to destroy everything the app had in memory?

有很多方法,例如 System.exit(),但通常建议您让系统按预期工作。

进一步阅读: http://developer.android.com/guide/components/processes-and-threads.html

关于android - 干净地退出android应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21122324/

相关文章:

c++ - 在 Android NDK 上使用 STL_tree.h 结果为 "No such file or directory"

android - 更新到 Android Studio 北极狐后,我无法使用任何自定义主题

iOS - 检测应用程序何时退出

php - 如何使用 Yii::app()->end() 方法以及它与 exit() 有何不同?

python - Python中exit()和sys.exit()的区别

python - 如何通过Python脚本退出正在运行的Windows程序?

android - 使用 Dagger2 将适配器注入(inject) fragment

java - 如何集成 Facebook api

android - JSONArray 无法转换为 JSONObject 错误

c++ - 为什么 gdb "u"命令不退出循环?