Asynctask class instance details

Asynctask, also known as "asynchronous task", is an abstract class

Asynctask specifies an abstract method for executing tasks in child threads. Developers can override this method in the implementation class of custom asynctask,

Then asynctask will automatically start the child thread to execute relevant code when working

Declaration of asynctask class:

public abstract class AsyncTask<Param,Progress,Result>

Param needs the data type of the parameter after executing the asynchronous task

Progress identifies the data type of progress during the execution of asynchronous tasks

Result the data type of the result to be returned after executing the asynchronous task

Abstract method in asynctask: public abstract result doinbackground (params... Params)

Get asynctask to work:

public final AsyncTask<params,Result> execute(params...params)

After the method is invoked, it automatically opens the child thread and invokes the dnInBackground () method, which must be invoked in the UI thread.

Case:

Layout:

MainActivity:

Asynctask update UI

Asynctask specifies the callback method after task execution. This method is not abstract, and developers can selectively implement it.

protected void onPostExecute(Result result)

This method is a method that runs on the main thread

example:

Layout:

MainActivity:

Asynctask update progress

Asynctask specifies the callback method for updating progress during task execution. This method is not abstract and can be selectively implemented by developers.

Protected void onprogressupdate (Progress... Values) (this method runs on the main thread)

During task execution, you can call the publishprogress () method to submit the progress, so that the onprogressupdate () method is called back

example

Layout:

LoadImage:

MainActivity:

Asynctask is a class that integrates task execution, progress update and result submission

It can centrally write all the code of an asynchronous task without caring about the communication between threads, which reduces the cost

Coding error probability, and effectively improve the readability and maintainability of the code.

Asynchronous loading pictures of small cases

Technologies used: canvas, paint

Canvas: used to determine the basic attributes of the canvas and perform painting

Paint: setting colors, fonts, and other settings

During the same drawing process, you may need to multiple brush objects or adjust the properties of the brush multiple times

Using canvas:

public Canvas() public Canvas(Bitmap bitmap) public void drawRect(float left,float top,float right,float bottom,Paint paint) public void drawBitmap(Bitmap bitmap,float left,Paint paint) public void drawText(String text,float x,float y,Paint paint)

Using paint:

public Paint() public native void setColr(int color) public native void setAntiAlias(boolean aa) public native void setTextSize(float textSize) public void setTextAlign(Align align) public Xfermode setXfermode(Xfermode xfermode)

summary

The above is all about the detailed explanation of asynctask class examples in this article. I hope it will be helpful to you. Welcome to our website: introduction to the file association method of Android development and implementation, detailed explanation of Android subcontracting multidex strategy, etc. you can leave a message at any time. Welcome to exchange and discuss.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>