Android Learning Notes-fifth chapter process and threads

Source: Internet
Author: User

Fifth chapter process and thread

  1. process: An application is a process

    (1) Priority of the process:

    Foreground Process Foreground processes

    A. The process in which the current user is operating the activity

    B. The process where the service of the activity with the current user action is bound

    C. The process in which the service with the priority is promoted by calling the Startforeground () method

    D. Calling the process where the service of the OnCreate (), OnStart (), Ondestory () method is located

    E. The broadcastreceiver in which the Onreceiver () method is being called

    Visiable process Visible

    A. The process in which the activity is in a paused state

    B. The process where the service of the activity that is in the suspended state is bound

    Service Process Services processes

    The process where the service started by calling the StartService () method

    Background Process Daemon

    The process in which the activity is in the stopped state

    Empty process

    Accelerates the start of the next program

  2. Threads; Sequential control flow within a program

    ANR: Application not responding application not responding

    main thread mechanism: when main threads execute for more than 5 seconds and do not respond to the next event, ANR may occur

    the role of the main thread: UI creation, updates, handling of events

    Only the original thread, created a view hierarchy can touch its views

    Only the thread that created the control will be able to update the control

  3. handler Messaging mechanism: implementing the UI interface in a newly created thread

    (1) MessageQueue Message Queuing:

    The MessageQueue is used for storing the message, and the stored message is executed in the FIFO (first in and out) principle. MessageQueue is encapsulated inside the looper.

    (2) Looper Circulator

    In Android, a thread corresponds to a Looper object, and a Looper object corresponds to a MessageQueue. The Looper object is used to open a message loop for a thread to manipulate the MessageQueue. By default, the newly created thread of Android does not have a message loop turned on (except the main thread). To create a handler object in a non-main thread, you first need to initialize a Looper object with the prepare () method of the Looper class, then create the handler object, and then use the Looper class's loop () method to start Looper, Gets and processes messages from the message queue. For example:

    public class Looperthread extends thread{

    Public Handler Handler;

    @Override

    public void Run () {

    Super.run ();

    Looper.prepare ();

    Instantiate a Handler object

    Handler = new Handler () {

    public void Handlemessage (Message msg) {

    LOG.I ("Looper", String.valueof (Msg.what));

    }

    };

    Message message = Handler.obtainmessage (); Get message

    Message.what = 0x11; Sets the value of the What property of message

    Handler.sendmessage (message); Send Message

    Looper.loop (); Start Looper

    }

    }

    Common methods provided by the Looper class

    A. Prepare (): Used to initialize Looper

    B. Loop (): After calling the Loop () method, the Looper thread starts to actually work, it gets the message from the message queue and processes the message

    C. Mylooper (): Can get the Looper object of the current thread

    D. GetThread (); To get the thread that the Looper object belongs to

    E. Quit (): Used to end the Looper loop

    Note: code written after Looper.loop () is not executed, and the function is internally a loop, and when the Handler.getlooper (). Quit () method is called, the Loop () method terminates and the code behind it can be run.

    (3) Handler Message processing class

    Handler allows you to send and process a message or Runnable object to the MessageQueue of the thread on which it resides. The main functions of handler are:

    A. Send the message or runnable application post () method or the SendMessage () method to MessageQueue, which can specify the delay time, send time, or bundle data to be carried. When MessageQueue loops to the message, it is called by the corresponding handler object's Handlermessage () method to process it.

    B. Communicate with the UI thread in a worker thread in a child thread and the main thread.

    Note: There can be only one looper and MessageQueue in a thread, however, there may be more than one handler, and these handler can share the same looper and MessageQueue.

    Handler common ways to send and process messages are:

    A. Handlemessage (Message msg): The method that processes the message. This method is typically overridden to process a message, and the method automatically callbacks when the message is sent.

    B. Post (Runnable R): Sends the Runnable object immediately, and the Runnable object is eventually encapsulated as a message object.

    C. postattime (Runnable R,long uptimemillis): Sending Runnable objects regularly

    D. postdelayed (Runnable r,long delaymillis): Delay delaymillis milliseconds to send Runnable object

    E. sendemptymessage (int): Send an empty message

    F. SendMessage (Message msg): Send Message now

    G. Sendmessageattime (Message msg, long Uptimemillis): Sending messages on a timed basis

    H. sendmessagedelayed (Message msg, long Delaymillis): How many milliseconds to delay sending messages

    (4) Message class

    The message is stored in MessageQueue, and a MessageQueue can contain multiple message objects. Each message object can be obtained through the message.obtain () or the Handler.obtainmessage () method. The Message object has the following 5 properties:

    A. Arg1:int type, used to store integer data

    B. Arg2;int type, used to store integer data

    C. Obj:object type, which holds arbitrary objects of type Object sent to the receiver

    D. Replyto;messenger type, which specifies the optional message object to where this message is sent

    E. What:int type, which specifies the user-defined message code so that the recipient can understand the information about the message.

    Note: You can use the properties of the message class to carry int data, and if you are carrying other types of data, you can save the data you want to carry to the bundle object, and then add it to the message by using the SetData () method of the message class.

    Attention:

    A. Typically, use the Message.obtian () method or the Handler.obtainmessage () method to obtain an empty message object from the message pool to conserve resources.

    B. If a message simply carries simple int information, it should take precedence over the MESSAGE.ARG1 and Message.arg2 properties to pass the information, which saves memory more than using bundles.

    C. Use Message.what as much as possible to identify information so that the message can be processed in different ways.


Android Learning Notes-fifth chapter process and threads

Related Article

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.