Android Inquiry Service (Beginner)

Service is a solution for running programs in the background in Android.

It is ideal for tasks that do not require user interaction and require long-running tasks.

The operation of the service does not depend on any user interface, and even if the program is switched to the background, or the user opens another program, the service can still keep running normally.

PS: The service does not run in an independent process, but depends on the application process in which the service was created.

When this application process is killed, all services that depend on this process will stop running.

What we need to do: create a child thread inside the service and perform specific tasks here.

2.1 Basic usage of threads

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);

// 1. The most basic thread inheritance starts
 new MyThread().start();        


// 2. Implement Runnable interface to start
         My2Thread my2Thread = new My2Thread();
         new Thread(my2Thread).start();


// 3. Anonymous classes are started directly, most commonly
 new Thread( new Runnable() {
             @Override
 public void run() {                    

            }
        });

    }

// 1. The most basic thread inherits Thread, high coupling
 class MyThread extends Thread{
         @Override
 public void run() {
             super .run();            
        }
    }

//    2.实现Runnable接口
class My2Thread implements Runnable{
        @Override
public void run() {            

        }

    }

2.2 Update UI in child thread

Like many other GUI libraries, Android's UI is thread-unsafe.

That is to say: if you want to update the UI of the program, you must do it in the main thread, otherwise an error will be reported.

But sometimes we have to perform some time-consuming operations in the child thread, and then update the UI according to the result of the operation. At this time, we need to use the asynchronous message processing mechanism.

2.3 Analysis of asynchronous message processing mechanism

Asynchronous message processing in Android mainly consists of 4 parts: Message  , Handler  , MessageQueue  and  Looper  .

  1. Message
    Message is a message passed between threads, which can carry a small amount of information internally for exchanging data between different threads.
  2. Handler
    Handler, as the name suggests, means processing, it is mainly used to send and process messages. Sending a message generally uses the Handler's sendMessage() method, and after a series of processing, the sent message will eventually be passed to the Handler's handlerMessage() method.
  3. MessageQueue
    MessageQueue means message queue, which is mainly used to store all messages sent through Handler. This part of the message will always exist in the message queue, waiting to be processed. There is only one MessageQueue object in each thread.
  4. Looper
    Looper is the MessageQueue housekeeper in each thread. After calling Looper's loop() method, it will enter an infinite loop, and then whenever a message is found in the MessageQueue, it will be taken out and passed to the Handler's handlerMessage() method. There will also only be one Looper object per thread.

The complete flow of asynchronous message processing:

First, you need to create a Handler object in the main thread and override the handlerMessage() method.

Then when a UI operation is required in the child thread, a Message object is created and the message is sent out through the Handler.

After this message will be added to the queue of MessageQueue waiting to be processed.

And Looper will always try to get pending messages from the MessageQueue.

The final distribution will be in the handlerMessage() method of Handler.

Since the Handler is actually created in the main thread, the code in the handleMessage() method will also run in the main thread at this time.

flow chart:



















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325540850&siteId=291194637