Benefits of parsing threads in Android

Source: Internet
Author: User

I. Android has two ways to process threads:

Time-consuming operations are placed in the background service, and the user is notified through the notification mechanism); time-consuming operations are processed in the background thread.

2. Use Handler

The best way to create backend threads is to create an instance of the Handler subclass. Only one Handler corresponds to one Activity. The custom background thread can communicate with Handler, which will work with the UI thread. Communication with Handler requires two options: message and runnable object.

Iii. Message

Send the Message to Handler. The first step is to call obtainMessage () to obtain the Message object from the pool.

Then, the Message can be sent to Handler through the Message queue, through sendMessage... () Method family:

SendMessage () sends the Message to the Message Queue immediately.

SendMessageAtFrontOfQueue () immediately sends a Message to the queue, which is placed at the beginning of the queue

SendMessageAtTime () sets the time to send a Message to the queue

SendMessageDelayed () sends a Message to the queue after several milliseconds of Delay

To process the Message, Handler needs to implement handleMessage (). When the Message appears in the queue, the handleMessage () method is called. In addition, Handler can update the UI as needed.

The following example shows a progress bar that increases by 1/20 units every second. :

 

Java code:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical"   
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent"> 
  6. <ProgressBar   
  7. android:id="@+id/progress" 
  8. style="?android:attr/progressBarStyleHorizontal"   
  9. android:layout_width="fill_parent" 
  10. android:layout_height="wrap_content" /> 
  11. </LinearLayout> 

Java code:

 
 
  1. package eoe.demo;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.os.Message;  
  6. import android.widget.ProgressBar;  
  7. public class ShowThread extends Activity {  
  8. ProgressBar bar;  
  9. Handler handler = new Handler() {  
  10. @Override  
  11. public void handleMessage(Message msg) {  
  12. bar.incrementProgressBy(5);  
  13. }  
  14. };  
  15. boolean isRunning = false;  
  16. /** Called when the activity is first created. */  
  17. @Override  
  18. public void onCreate(Bundle savedInstanceState) {  
  19. super.onCreate(savedInstanceState);  
  20. setContentView(R.layout.main);  
  21. bar = (ProgressBar) findViewById(R.id.progress);  
  22. }  
  23. @Override  
  24. protected void onStart() {  
  25. super.onStart();  
  26. bar.setProgress(0);  
  27. Thread background = new Thread(new Runnable() {  
  28. @Override  
  29. public void run() {  
  30. for (int i = 0; i < 20 && isRunning; i++) {  
  31. try {  
  32. Thread.sleep(1000);  
  33. } catch (InterruptedException e) {  
  34. }  
  35. handler.sendMessage(handler.obtainMessage());  
  36. }  
  37. }  
  38. });  
  39. isRunning = true;  
  40. background.start();  
  41. }  
  42. @Override  
  43. protected void onStop() {  
  44. super.onStop();  
  45. isRunning = false;  
  46. }  

Summary:

When an android program is started for the first time, android automatically creates a thread called "main" main thread. This thread is very important, because it is responsible for assigning the time to the corresponding control, including the screen drawing time, which is also the thread for the user to interact with the android control.

Android Timer usage

Android smartphone operating system

Common Android commands and simulator Parameters

Use C # to implement multi-threaded file transmission over HTTP

SQL bit by bit collection SQL Server thread wait Information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.