Android:Async Task返回值会发生什么

时间:2014-07-10 04:58:44

标签: android android-asynctask android-async-http

如果已调用的应用程序已完成,则返回值或调用Aysnc任务会发生什么。

假设我在Aysnc任务中调用网络操作,并且调用它的UI线程已完成操作。

我的目标是保存在N / W呼叫中收到的数据,以便我下次需要重新拨打电话?

下面是代码片段,其中在doInBackground()中我发出了SOAP请求,当请求打开时,调用Aync任务的主UI以某种方式死亡/完成。我的回复会怎样?如何保存响应以备将来使用。

    Code:

        class SOAPCallAyncTask1 extends AsyncTask<String, String, String> {

    ProgressDialog pd = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(SoapMainActivity.this);
        pd.setMessage("Please wait...");
        pd.setCancelable(false);
        pd.show();

    }

    @Override
    protected String doInBackground(String... params) {

        String theResult = "";
        byte[] result = null;

        try {
            result = callSOAPServer();
            theResult = new String(result);
            Log.d("R-Doit in backgound", theResult);
        }

        catch (Exception e) {
            e.printStackTrace();
        }

        return theResult;
    }

    @Override
    protected void onPostExecute(String theResult) {
        super.onPostExecute(theResult);
        pd.dismiss();
        Log.d("R-Result", theResult);
    }

}

1 个答案:

答案 0 :(得分:0)

即使应用程序被关闭/销毁,AsyncTask也将完成执行,除非系统要求它停止执行(通常不是这种情况)。

您可以在SharedPreferences中保存网络调用中收到的数据,然后在创建活动时(或在onResume())中再次(如果需要)通过获取活动来更新UI已保存SharedPreferences的数据。如果您发现在上一次AsyncTask执行期间未提取任何数据,则可以相应地生成新的执行请求。

相关问题