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:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ProgressBar
- android:id="@+id/progress"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
Java code:
- package eoe.demo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.ProgressBar;
- public class ShowThread extends Activity {
- ProgressBar bar;
- Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- bar.incrementProgressBy(5);
- }
- };
- boolean isRunning = false;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- bar = (ProgressBar) findViewById(R.id.progress);
- }
- @Override
- protected void onStart() {
- super.onStart();
- bar.setProgress(0);
- Thread background = new Thread(new Runnable() {
- @Override
- public void run() {
- for (int i = 0; i < 20 && isRunning; i++) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- handler.sendMessage(handler.obtainMessage());
- }
- }
- });
- isRunning = true;
- background.start();
- }
- @Override
- protected void onStop() {
- super.onStop();
- isRunning = false;
- }
- }
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