Communication between activity, service, and thread in Android

Source: Internet
Author: User
Activity, service, and thread should be the most common types of Android programming. Almost most applications will involve these types of programming. Naturally, communication between the three will be involved, this article attempts to briefly introduce the three communication methods. The reason I want to write this article is that I am working on an android application with a few colleagues. The code was messy at first, because I directly created a thread in the activity, to execute some tasks. However, we know that the thread may take a long time to run, and Android will destroy some activities when the memory is insufficient, so that the thread will lose the management object, in this way, unexpected results may occur in the program. In addition, in the activity to create a thread, the thread and activity communication is also more troublesome, generally with the handler class to communicate (http://blog.sina.com.cn/s/blog_3fe961ae0100mvc5.html ). Compared with the activity, the Service generally runs in the background silently and has a long life cycle. Therefore, it is more suitable to provide some services for the main program, create threads, and manage threads. Therefore, I changed the original program to a three-tier architecture, from high to low: Activity layer-service layer-thread layer. Activity "tells" the service layer of the required service, and creates a thread on the service layer to complete the service. The thread sends the progress, status, and error information of the task to the service. The service sends these messages to the related activity, and the notification can be used to update the notification bar message. This is the general structure.
1 activity and service communication 1.1 using Handler Communication: http://blog.sina.com.cn/s/blog_3fe961ae0100mvc5.html
1.2 activity calls the startservice (intentservice) method to add the message to the intent object, so that the service object can get the message when calling onstartcommand (intentintent, int flags, intstartid. This method is very simple, but if there is a lot of information to pass, it will be very troublesome. The service end must determine what the message is before further action can be performed.
1.3 activity calls the bindservice (intentservice, serviceconnection Conn, intflags) method to obtain a reference to the service object, so that the activity can directly call the method in the service. Code: http://blog.csdn.net/liuhe688/article/details/6623924.
1.4 Service sends messages to activity. In addition to handler, broadcast can also be used. Of course, activity must register the corresponding receiver. For example, it is better to use this method to send the same message to multiple activities. For details, refer to this article: http://blog.csdn.net/liuhe688/article/details/6641806.
2 Communication between service and thread 2.1 after a thread is created, if you want to control the thread (START, pause, stop, etc.), the reference of the thread should be retained in the service, needless to say. With this reference, the service can directly call other methods of thread. To send messages to the service, the running thread usually uses handler: http://blog.sina.com.cn/s/blog_3fe961ae0100mvc5.html.

3. Do not think much about the communication between activity and thread. Use handler directly. It is not recommended that the activity directly create a thread because it is difficult to manage the thread.


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.