Android AsyncTask调用onProgressUpdate或onPostExecute延迟 [英] Android AsyncTask call onProgressUpdate or onPostExecute delay

查看:91
本文介绍了Android AsyncTask调用onProgressUpdate或onPostExecute延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    class DoLogin extends AsyncTask<String, Integer, String> {// 继承AsyncTask
    @Override
    protected String doInBackground(String... params) {// 处理后台执行的任务,在后台线程执行
        ServerAnalyze.log("消息", "登陆", "手动登陆函数开始启动.");

        EditText etStuId = (EditText) findViewById(R.id.tbStudentID);
        EditText etPwd = (EditText) findViewById(R.id.tbPassword);

        global.setStudentID(etStuId.getText().toString());
        String Password = etPwd.getText().toString();
        global.setImei(((TelephonyManager) getSystemService(TELEPHONY_SERVICE))
                .getDeviceId());
        Log.i("loginclick", "bg0");
        global.giitServiceInterface.SendBroadCast_Login(global.getStudentID(), Password, global.getImei());
        Log.i("loginclick", "bg1");
        publishProgress(0);
        Log.i("loginclick", "bg2");
        return "";
    }

    protected void onProgressUpdate(Integer... progress) {// 在调用publishProgress之后被调用,在ui线程执行
        Log.i("loginclick", "update");
    }

    @Override
    protected void onPostExecute(String result) {// 后台任务执行完之后被调用,在ui线程执行
        // progressDialog.dismiss();
        Log.i("loginclick", "finish");
    }

    @Override
    protected void onPreExecute() {// 在doInBackground(Params...)之前被调用,在ui线程执行

        Button btnloginButton = (Button) findViewById(R.id.btnlogin);
        btnloginButton.setText("正在登陆中,请稍候...");
        btnloginButton.setBackgroundColor(getResources().getColor(
                R.color.huise));
    }

    protected void onCancelled() {// 在ui线程执行

    }

}

09-22 13:13:09.753: I/loginclick(1751): bg0
09-22 13:13:09.763: I/loginclick(1751): bg1
09-22 13:13:09.763: I/loginclick(1751): bg2
09-22 13:13:09.773: I/Giit Service(1751): Start
09-22 13:13:24.824: I/loginclick(1751): update
09-22 13:13:24.824: I/loginclick(1751): finish
09-22 13:13:24.834: I/[GiitParamClass-Action](1751): 1
09-22 13:13:24.844: I/[GiitParamClass-BooleanResult](1751): false
09-22 13:13:24.844: I/[GiitParamClass-intResult](1751): 0

这样的节目:

我有一个问题.

为什么已记录bg0,bg1,b2,但是onProgressUpdate和onPostExecute需要服务回调,然后它们才能运行.

why bg0,bg1,b2 have been log,but onProgressUpdate and onPostExecute need service callback then them run.

09-22 13:13:09服务启动

09-22 13:13:09 service start

但仍然是09-22 13:13:24.824 BroadCast回叫,然后onProgressUpdate和onPostExecute开始运行.

but still 09-22 13:13:24.824 BroadCast call back then onProgressUpdate and onPostExecute start run.

我想知道为什么会导致这种情况.

I want to know,why cause that.

我尝试搜索,但是我不知道如何写一个很好的关键字.因此我没有找到答案.

I try to search,but I don't known how to write a nice keyword.so I not found anwser.

真诚的感谢.

推荐答案

在AsycTask中

这些函数的调用顺序为

随着 doInBackground(Params ...)完成某些单元任务,它将把它推送到 publishProgress(Progress ...),并调用onProgressUpdate(Progress ...)更新用户界面

As doInBackground(Params...) complete some unit task it'll push it to publishProgress(Progress...) and it calls onProgressUpdate(Progress...) to update teh UI

1 onPreExecute(),在执行任务之前在UI线程上调用.此步骤通常用于设置任务,例如,通过在用户界面中显示进度栏.

1 onPreExecute() , invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

2.) doInBackground(Params ...),在onPreExecute()完成执行后立即在后台线程上调用.此步骤用于执行可能需要很长时间的后台计算.异步任务的参数将传递到此步骤.

2.) doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step.

此步骤必须返回计算结果,并将其传递回最后一步.

The result of the computation must be returned by this step and will be passed back to the last step.

此步骤还可以使用 publishProgress(Progress ...)发布一个或多个进度单位.这些值在onProgressUpdate(Progress ...)步骤中发布在UI线程上.

This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

3.) onProgressUpdate(Progress ...),在调用publishProgress(Progress ...)之后在UI线程上调用.

3.) onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...).

执行时间不确定.此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度.例如,它可以用于为进度栏设置动画或在文本字段中显示日志.

The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

4.) onPostExecute(Result),在后台计算完成后在UI线程上调用.后台计算的结果作为参数传递到此步骤.

4.) onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

这篇关于Android AsyncTask调用onProgressUpdate或onPostExecute延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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