完全退出Android应用 [英] Cleanly exit android app

查看:208
本文介绍了完全退出Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我已经看了这个主题说(恢复):呼叫完成()您的活动,如果您有运行任何业务,呼叫 stopService()上他们。

所以,我做了,因为它似乎工作,我不再担心。今天,调试我的code的一部分,我需要添加一个登录线进我希望被执行的所有应用程序的生命非结束线程的方法,基本上它是这样的:

 最终螺纹buf_liberator =新主题(
  新的Runnable(){
    公共无效的run(){
      而(真){
        methodCall();
        SystemClock.sleep(9000);
      }
    }
  }
);
buf_liberator.setPriority(7);
buf_liberator.start();

methodCall()我把登录行,并为我惊讶的是,当我关闭我的应用程序推这就要求完成()并停止我已经开始了一个服务上的退出按钮,我仍然看到登录消息每次9秒无限期地在LogCat中。

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

谢谢!


解决方案

  

所以,问题(s)是(有):这个线程是不是一种服务,它为什么仍然运行


由于您的进程仍在运行。即使当你完成()的活动或停止()服务,您的申请过程可能会继续运行。


  

我是不是做错了什么?


除了创造一个永无止境的主题?没有。


  

这是一个预期的行为?


是的。 Android系统不断的过程四周,缓存机制,加快恢复previously使用的应用程序。其认为必要的系统最终会杀死你的进程(与正在运行的线程)。的不要指望你的线程没有结束,因为它会最终


  

有没有毁灭一切的应用程序不得不在内存中真正的方法是什么?


有办法,像System.exit(),但它通常建议您让系统工作按预期。

延伸阅读:
<一href=\"http://developer.android.com/guide/components/processes-and-threads.html\">http://developer.android.com/guide/components/processes-and-threads.html

Anywhere I've looked for this topic says (resuming): "call finish() on your activity and if you have any services running, call stopService() on them".

So that I did, and as it seemed to work, I stopped worrying. Today, debugging a part of my code, I needed to add a Log line into a non-ending thread's method which I want to be executed all the app's life, basically it's something like this:

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

In methodCall() I put the Log line, and for my surprise, after I closed my app pushing the Exit button which calls finish() and stops a service I've started, I still see the Log message every 9 seconds indefinitely in the LogCat.

So the question(s) is(are): This thread is not a service, why it keeps running? Am I doing something wrong? Is this an expected behavior? Is there a real method to destroy everything the app had in memory?

Thanks!

解决方案

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

Because your process is still running. Even when you finish() an Activity or stop() a Service, your application process may keep running.

Am I doing something wrong?

Besides creating a never-ending thread? No.

Is this an expected behavior?

Yes. The Android system keeps process around as a caching mechanism to speed up resuming previously used apps. The system will eventually kill your process (and the running thread) as it deems necessary. Do not count on your thread not ending, because it will, eventually

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

There are ways, like System.exit(), but it's generally advised that you let the system work as intended.

Further Reading: http://developer.android.com/guide/components/processes-and-threads.html

这篇关于完全退出Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆