Android AsyncTask usage
Comparison between AsyncTask and Handler
1) AsyncTask implementation principle, and applicable advantages and disadvantages
AsyncTask is a lightweight asynchronous class provided by android. It can inherit AsyncTask directly and implement asynchronous operations in the class,AndProvide interface feedback to the currentAsynchronous execution degree(UI progress update can be implemented through the interface), and finally the execution result is fed back to the main UI thread.
Advantages:
L simple and fast
L process controllable
Disadvantages:
L it becomes complicated when multiple asynchronous operations and Ui changes are required.
2) principle and advantages and disadvantages of Handler asynchronous implementation
When Handler is implemented asynchronously, it involves four objects: Handler, logoff, Message, and Thread. the asynchronous process is that the main Thread starts thread (subthread) à Thread (subthread) run and generate Message-à logoff to get the Message and pass it to Handler. the Handler gets the Message in logoff one by one and changes the UI.
Advantages:
L clear structure and clear Function Definition
L simple and clear for multiple background tasks
Disadvantages:
L in asynchronous processing of a single backend, there are too many codes and the structure is too complex (relative)
Introduction to AsyncTaskAndroid AsyncTask is more lightweight than Handler and is suitable for simple asynchronous processing. First, it is clear that Android has Handler and AsyncTask to avoid blocking the main thread (UI thread), and UI Updates can only be completed in the main thread, so asynchronous processing is inevitable.
Android provides AsyncTask to reduce the development difficulty. AsyncTask is a encapsulated background task class. as its name implies, it is an asynchronous task.
AsyncTask directly inherits from the Object class and is located in android. OS. AsyncTask. To use AsyncTask, we need to provide three generic parameters and reload several methods (at least one ).
AsyncTask defines three generic types: Params, SS, and Result.