Android应用好好尝试一下电话"的onDestroy()"当杀死(ICS) [英] Android app doens't call "onDestroy()" when killed (ICS)

查看:148
本文介绍了Android应用好好尝试一下电话"的onDestroy()"当杀死(ICS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用蓝牙通信(使用propetary协议)的Andr​​oid应用程序,我需要抓到的那一刻,当应用程序被杀害。

I'm developing an android app using bluetooth communication (using a propetary protocol) and I need to catch the moment when the app is killed.

我想用的onDestroy()方法,但它不叫每一个应用程序被杀害的时间。 我注意到,它被称为当我preSS后退按钮,只是有时,当我杀死从任务管理器应用程序。

I wanted to use the "onDestroy()" method but it isn't called every time the app is killed. I noticed that it is called when I press the back button and, only sometimes, when I kill the app from the task manager.

现在的问题是:我如何能赶上那一刻的应用程序被杀害之前

The question is: How can I catch the moment before the app is killed?

下面是code我试图用:

Here is the code I tried to use:

@Override
public void onDestroy() {
    sendMessage(msg);
    Log.d("SampleApp", "destroy");
    super.onDestroy();
}

@Override
public void finish(){

    sendMessage(msg);
    Log.d("SampleApp", "finish");
    super.finish();
}

不幸的是完成()不会被调用和的onDestroy不叫,每次我关闭从任务管理器应用程序。

Unfortunately finish() is never called and onDestroy isn't called every time I close the app from the task manager.

我该如何处理呢?

推荐答案

由于文件的此处,没有保证的onDestroy()永远不会被调用。相反,使用的onPause()做你想要做的事情每当应用程序移动到背景,只留下了code中的onDestroy(),您需要时运行你的应用程序被杀害。

As stated in the documentation here, there is no guarantee that onDestroy() will ever be called. Instead, use onPause() to do the things you want to do whenever the app moves into the background, and leave only that code in onDestroy() that you want run when your app is killed.

编辑:

从您的意见,似乎要运行一些code,只要您的应用程序进入后台,但如果它进入了后台,因为你发起的意图。 AFAIK,还有Android中,默认情况下处理这个没有办法,但你可以使用这样的:

From your comments, it seems that you want to run some code whenever your app goes into the background, but not if it went into the background because you launched an intent. AFAIK, there is no method in Android that handles this by default, but you can use something like this:

有这样一个布尔值:

boolean usedIntent = false;

现在使用的意图之前,设置布尔为true。现在,在你的onPause(),将code为目的的情况下到,如果像这样一个块:

Now before using an intent, set the boolean to true. Now in your onPause(), move the code for the intent case into an if block like this one:

if(usedIntent)
{
//Your code
}

最后,在你的onResume(),请重新设置布尔值false,所以它可以与你的应用程序移动到背景中由非意向协议意味着正常。

Finally, in your onResume(), set the boolean to false again so that it can deal with your app being moved into the background by a non intent means properly.

这篇关于Android应用好好尝试一下电话"的onDestroy()"当杀死(ICS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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