In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to achieve multithreading in the background in Android". In daily operation, I believe that many people have doubts about how to achieve multithreading in the background in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to achieve multithreading in the background in Android". Next, please follow the editor to study!
Background function is one of the four major components of Android, its importance is self-evident, and service is the solution to realize the background running of programs in Android, which is very suitable for performing tasks that do not require interaction and have to run for a long time. By default, all tasks are completed in the main thread, that is, if we do not have multithreaded programming, it will cause the main thread to be blocked.
Multithreading
New Thread (new Runnable () {@ Override public void run () {}}) .start ()
New threads can be created through new Thread
Class MyThread1 implement Runnable {@ Override public void run () {}} new Thread (new MyThread ()) .start ()
By implementing the Runnable interface
Class MyThread1 extends Thread {@ Override public void run () {}} newMyThread.start ()
By inheriting Thread
Update UI when using multithreading:
RunOnUiThread (new Runnable () {public void run () {textView.setText (result);}})
Of course, Android also provides a set of asynchronous message handling mechanisms:
Public class MainActivity extends AppCompatActivity {private Handler handler=new Handler () {@ Override public void handleMessage (Message msg) {TextView textView= (TextView) findViewById (R.id.tvMsg); textView.setText (msg.obj.toString ());}}; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) Button button= (Button) findViewById (R.id.button); button.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {new Thread (new Runnable () {@ Override public void run () {Message message=new Message ()) Message.obj=new String (data); handler.sendMessage (message);}}) .start ();}});}}
Among them
Message is used to encapsulate messages.
Handle is used to send and process messages, calling sendMessage in child threads to send messages, and processing in HandleMessage in the main program
MessageQueu, which saves messages sent by handle waiting to be processed
Looper completes the message dispatch task, constantly checks whether there are any messages in the queue, and if so, extracts them to handle.
AsyncTask
Is an abstract class provided by Android simplified thread data update UI
OnPreExecute (): called before asynchronous execution starts
OnPostExecute (): called after the end of asynchronous execution
OnProgressUpdate
DoInBackground
Use servic
Start the service through startService, start it if there is no service, create it, call oncreat when you create it, and call onStartCommand every time startService is called
Through stopService or stopSelf, onDestroy is executed
Bind service
Use bindService to bind services
UnbindService unbinds
At this point, the study on "how to implement multithreading in the background in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.