Java 在AsyncTask中重写前/后执行并调用super.onPre/PostExecute

Java 在AsyncTask中重写前/后执行并调用super.onPre/PostExecute,java,android,android-asynctask,Java,Android,Android Asynctask,在异步任务中重写onPreExecute时是否必须调用super.onPreExecute? AsyncTask.onPreExecute和其他方法实际上做什么? onPostExecute和onCancelled的问题相同 public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> { @Override protected void onCancelled(Boolean result) { sup

在异步任务中重写onPreExecute时是否必须调用super.onPreExecute? AsyncTask.onPreExecute和其他方法实际上做什么? onPostExecute和onCancelled的问题相同

public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> 
{

@Override
protected void onCancelled(Boolean result) {

    super.onCancelled(result);   //<-DO I HAVE TO?

            //My onCancelled code below


}

@Override
protected void onPostExecute(Boolean result) {

    super.onPostExecute(result);  //<-DO I HAVE TO?

            //My onPostExecute code below
}

@Override
protected void onPreExecute() {

    super.onPreExecute();  //<-DO I HAVE TO?

            //My onPreExecute code below

}

@Override
protected Boolean doInBackground(Void... params) {

    return null;
}
公共类MyAsyncTask扩展了AsyncTask
{
@凌驾
取消时受保护的void(布尔结果){

super.onCancelled(result);//不,您不需要调用
super

正如您所看到的,默认实现什么都不做

/**
* Runs on the UI thread before {@link #doInBackground}.
*
* @see #onPostExecute
* @see #doInBackground
*/
protected void onPreExecute() {
}

/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param result The result of the operation computed by {@link #doInBackground}.
*
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@SuppressWarnings({"UnusedDeclaration"})
protected void onPostExecute(Result result) {
}
/**
*在{@link#doInBackground}之前的UI线程上运行。
*
*@see#onpost执行
*@see#doin background
*/
受保护的void onPreExecute(){
}
/**
*在{@link#doInBackground}之后的UI线程上运行
*指定的结果是{@link#doInBackground}返回的值

* *如果任务被取消,则不会调用此方法

* *@param result由{@link#doInBackground}计算的操作结果。 * *@see#onPreExecute *@see#doin background *@see#onCancelled(对象) */ @SuppressWarnings({“UnusedDeclaration”}) 受保护的void onPostExecute(结果){ }
不,我们不需要强制重写这些方法 onPreExecute和onPostExecute。

onPreExecutedoInbackground过程开始之前调用,我们可以在这个方法中添加代码,在任何事情开始工作doInbackground之前我们必须这样做

doInbackground 在后台工作,所以在这个方法中,我们可以在后台做任何我们想做的事情,比如调用web服务和所有东西。但是,注意这个方法不能在这个方法中设置UI小部件。如果设置了,它将给出异常

onPostExecutedoInbackground上完成后调用&在这个方法中,我们可以设置UI小部件和其他代码,以便在Webservice调用完成后进行设置

一旦取消 可以通过调用cancel(布尔值)随时取消任务。调用此方法将导致对isCancelled()的后续调用返回true

检查链接。
希望这能对您有所帮助。

我在询问基类的实现,但无论如何还是要感谢您抽出时间。如果以后更新了
AsyncTask
,仍然调用
super
不是一个好做法吗?现在没有任何内容,但是如果他们更改它会怎么样。。。