The Android SDK provides a way to isolate certain operations from the main UI thread:
- The Asynctask class, which completes the asynchronous operation and maintains communication with the main UI thread
- Standard thread class completes asynchronous operation
- Use loader to perform data loading in activity or fragment to ensure speed of operation
The action of blocking the thread will result in a ANR behavior (application not responding)
Common blocking threads include the following:
- All too long or responsible calculations or operations
- Request for variable-length data sets
- Parsing a data set
- Working with multimedia files, example, video, or audio
- Iterate over variable-length data structures
- Get network resources
- Get Location Services
- Get the content Provider interface
- Access Local Database
- Access Local Files
- Access tasks include services with the above content
Asynctask callback method:
OnPreExecute () runs on the UI thread before the background operation is complete;
Doinbackground () runs in the background and handles background operations;
Calling the Publishprocess () method from Doinbackground () periodically notifies the UI thread about the progress of the background operation. This method sends a message to the UI action and takes this opportunity to update the user-visible progress bar. Onprocessupdate () runs in the UI thread;
The background operation is complete, OnPostExecute () runs on the UI thread;
Run execute (), Asynctask handles the operation in the background without affecting the UI thread;
Asynctask Task Start Method:
- Execute () method, where each task instance is executed once, like using a thread pool;
- Executeonexector (asynctask.thread_pool_executor,id); Perform tasks by ID
"Android" Threads and asynchronous operations