即使应用程序被破坏的AsyncTask将始终运行? [英] AsyncTask will always run even if app is destroyed?

查看:113
本文介绍了即使应用程序被破坏的AsyncTask将始终运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,因为你不能做网络操作使用AsyncTask的主线程IM,这样的问题是,一旦我的execute()的AsyncTask的右后,我完成()的活性,也许用户会完成( )在整个应用程序,还等什么即时知道的是:

  1. 将AsyncTask的总是完成doInBackground()和onPostExecute(),即使该应用程序是只要执行(关闭)被调用的应用程序正在运行的时候?
解决方案

您将可以进行测试。是的它。如果执行被称为可以看到的AsyncTask仍将执行,除非它做了一些相关于地面或UI。 (它可能会导致启动器崩溃)。


但是,如果它是在附近的系统。它可以或可以不继续执行此方法。我已经测试并回答<一href="http://stackoverflow.com/questions/14611893/when-android-kills-an-app-can-a-function-be-stopped-halfway/14611934#14611934">here. 回复评论: 测试:

  @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        新工人()执行()。
    }
私有类工人扩展的AsyncTask&LT;虚空,虚空,字符串&GT; {

    @覆盖
    保护字符串doInBackground(虚空......为arg0){
        Log.i(SomeTag
                开始做在背景在+ System.currentTimeMillis的());
        字符串数据= NULL;

        尝试 {
            DefaultHttpClient的HttpClient =新DefaultHttpClient();
            HTTPGET HTTPGET =新HTTPGET(
                    http://stackoverflow.com/questions/tagged/android);

            HTT presponse HTT presponse = httpClient.execute(HTTPGET);
            HttpEntity httpEntity = HTT presponse.getEntity();
            数据= EntityUtils.toString(httpEntity);
            Log.i(SomeTag
                    doInBackGround做的+ System.currentTimeMillis的());
        }赶上(例外五){
        }
        返回的数据;
    }

    @覆盖
    保护无效onPostExecute(字符串结果){
        super.onPostExecute(结果);
        Log.i(SomeTag,System.currentTimeMillis的()/ 1000L
                +后执行的\ n+结果);
    }
}

@覆盖
保护无效的onDestroy(){
    super.onDestroy();
    Log.i(SomeTag,System.currentTimeMillis的()/ 1000L +onDestory());
}

04-24 21:42:57.981:I / SomeTag(5961):开始做后台以1366854177994
04-24 21:43:00.974:I / SomeTag(5961):1366854180 onDestory()
04-24 21:43:02.946:I / SomeTag(5961):doInBackGround完成在1366854182946
04-24 21:43:02.946:I / SomeTag(5961):1366854182后执行
04-24 21:43:02.946:I / SomeTag(5961):&LT; D​​OCTYPE HTML&GT;
04-24 21:43:02.946:I / SomeTag(5961):其中,HTML&GT;
04-24 21:43:02.946:I / SomeTag(5961):其中; HEAD&GT;
04-24 21:43:02.946:I / SomeTag(5961):
04-24 21:43:02.946:I / SomeTag(5961):其中,标题&GT;最新&安培;#39;机器人和放大器;#39;问题 - 堆栈溢出&LT; /标题&GT;
04-24 21:43:02.946:I / SomeTag(5961):其中,链接相对=快捷方式图标的href =htt​​p://cdn.sstatic.net/stackoverflow/img/favicon.ico&GT;
// ....
 

i have an application and because you cant do network operations on the main thread im using AsyncTask, so the question is once i execute() the AsyncTask right after that i finish() the activity, and maybe the user will finish() the whole app, so what im wondering is:

  1. will AsyncTask always finish doInBackground() and onPostExecute() even if the app is closed as long as execute() was called when the app was running?

解决方案

You will be able to test this. And yes It does. If execute was called you can see Asynctask will still execute UNLESS it does something to the forground or UI related. (it may cause launcher to crash).


However, if it was close by the system. It may or may not continue executing the method. I have already tested and answered here. Reply to comment: Tested:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Worker().execute();
    }
private class Worker extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... arg0) {
        Log.i("SomeTag",
                "start do in background at " + System.currentTimeMillis());
        String data = null;

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(
                    "http://stackoverflow.com/questions/tagged/android");

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            data = EntityUtils.toString(httpEntity);
            Log.i("SomeTag",
                    "doInBackGround done at " + System.currentTimeMillis());
        } catch (Exception e) {
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.i("SomeTag", System.currentTimeMillis() / 1000L
                + " post execute \n" + result);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.i("SomeTag", System.currentTimeMillis() / 1000L + "  onDestory()");
}

04-24 21:42:57.981: I/SomeTag(5961): start do in background at 1366854177994
04-24 21:43:00.974: I/SomeTag(5961): 1366854180  onDestory()
04-24 21:43:02.946: I/SomeTag(5961): doInBackGround done at 1366854182946
04-24 21:43:02.946: I/SomeTag(5961): 1366854182 post execute 
04-24 21:43:02.946: I/SomeTag(5961): <!DOCTYPE html>
04-24 21:43:02.946: I/SomeTag(5961): <html>
04-24 21:43:02.946: I/SomeTag(5961): <head>
04-24 21:43:02.946: I/SomeTag(5961):         
04-24 21:43:02.946: I/SomeTag(5961):     <title>Newest &#39;android&#39; Questions - Stack Overflow</title>
04-24 21:43:02.946: I/SomeTag(5961):     <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">
//....

这篇关于即使应用程序被破坏的AsyncTask将始终运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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