即使应用程序被销毁,AsyncTask 也将始终运行? [英] AsyncTask will always run even if app is destroyed?

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

问题描述

我有一个应用程序,因为你不能在主线程上进行网络操作,所以我使用 AsyncTask,所以问题是一旦我 execute()AsyncTask 然后我 finish() 活动,也许用户会 finish() 整个应用程序,所以我想知道的是:

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

  1. AsyncTask 总是完成 doInBackground()onPostExecute() 即使应用程序关闭,只要 execute() 在应用运行时被调用?
  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?

推荐答案

您将能够对此进行测试.是的,确实如此.如果调用了 execute ,您可以看到 Asynctask 仍将执行,除非它对前台或 UI 相关执行某些操作.(这可能会导致启动器崩溃).

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).


但是,如果它靠近系统.它可能会也可能不会继续执行该方法.我已经测试并回答了 这里.回复评论:已测试:

@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(
                    "https://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 
" + 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天全站免登陆