android – 为什么不会调用AsyncTask <>接口的onProgressUpdate()?

前端之家收集整理的这篇文章主要介绍了android – 为什么不会调用AsyncTask <>接口的onProgressUpdate()?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
onProgressUpdate()从来没有被调用过,你能告诉我为什么吗?
private class LoginMe extends AsyncTask<Context,Void,Boolean> {

    @Override
    protected Boolean doInBackground(Context... arg0) {
        doSomething();
        return true;
    }

    @Override
    protected void onProgressUpdate(Void... v) {
        super.onProgressUpdate(v);
        Log.d("Dev","Im in onProgressUpdate()");
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (result) {
            doOne();
        } else {
            doTwo();
        }
    }
}

解决方法

您必须手动从doInBackground中调用publishProgress.

doInBackground(Params…),invoked on
the background thread immediately
after onPreExecute() finishes
executing. … 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.

http://developer.android.com/reference/android/os/AsyncTask.html

猜你在找的Android相关文章