Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use AnsyncTask to implement Asynchronous tasks in Android

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use AnsyncTask to achieve asynchronous tasks in Android". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The conceptual difference between synchronous and asynchronous:

Synchronization, you must execute a problem before you can proceed with anything else.

Asynchronously, I will execute other questions first, and then you can return a result to me after you finish.

Why do you reference asynchronous tasks in android?

When android starts, it starts a thread, also known as the main thread, the UI thread, but we can't leave all the time-consuming tasks to the main thread, which will affect the user's experience, that is to say, we have to open up new threads to perform our tasks.

But in android, (loading pictures and network programming are time-consuming operations), it is forbidden to complete network operations in the main thread after android4.0, so these time-consuming tasks are left to child threads.

Problem, the ui operation cannot be performed in the child thread, and only the UI thread as the main thread can perform the ui operation, so how do we return the information to the user?

Method 1. Through handler, we can write the new operation in handler and call sendmessage in the child thread to notify our handler and the new ui.

Method 2. Use Activity.runonUIThread (Runnable) to switch from child thread to UI thread.

Why use AsyncTask?

Very simply, people are very lazy, and it is troublesome to use the above two methods, so the bosses encapsulate a method of asynchronous execution, so it is convenient for us to directly deal with time-consuming logic and update UI.

We generally use it to complete network operations, picture loading, data transmission and other operations. (of course, we will choose our third-party framework to do it later.)

Basic use of Asynctask:

First of all, it's an abstract class, which means we have to rewrite its method and then in new

AsyncTask

Parames: the parameter that is executed when the task is started, such as the url passed in

Progress: the percentage of progress in background execution

Result: the result returned after performing the asynchronous operation

(compared with handler, his background is a thread pool, which shows its advantage when the data is huge.)

Several important methods of AsyncTask

@ Override protected void onPreExecute () {super.onPreExecute ();} / / A method called before performing a time-consuming operation, which is equivalent to our initialization method

Note that the method is running in the ui thread

@ Override protected String doInBackground (String... Params) {return null;}

This party is responsible for performing our time-consuming business logic operations, that is to say, it runs in child threads. What if you want to update ui?

Call our publicProgress method to update our ui, which is run in a child thread

PublishProgress (Object o)

This method indicates to update our progress, can be used to update the download progress bar, and so on.

@ Override protected void onProgressUpdate (Integer... Values) {super.onProgressUpdate (values);}

This method is executed in the main thread, and once we call publicProgress in the time-consuming logic code, it indicates that we want to update the progress bar.

This method will be called to the UI thread to update our progress bar.

Note: the task instance must create a task.excute in the ui thread (initial parameters such as url)

We do not need to manually call methods in task

Task can only be executed once

This is the end of "how to use AnsyncTask to implement asynchronous tasks in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report