Simple Application of broadcastreceiver and broadcast in Android

Source: Internet
Author: User

Recently, Android IM systems often need to use background programs to refresh the foreground interface. We know that there is no interface for the background program service, and the service cannot call the foreground space, so how do we refresh the front-end interface? I have read the following two blog articles: "android journey-broadcast (broadcast)" and "android UI design and background thread interaction", and wrote related demos. The following is my summary.

Function of broadcast:

(1) implement data transmission and sharing between different programs, because as long as it is the same as the receiver of the send broadcast action, it can accept the broadcast.

(2) It serves as a notification, such as notifying the main program in the service and updating the UI of the main program.

(3) broadcast uses intent to transmit data.

Broadcast implementation:

Before that, let's talk about how to filter broadcasts? How does acitvity Accept Broadcast?

We know that there are two ways to register broadcast. The first is static registration. You need to configure relevant attributes in androidmainfes. XML, and the second is to dynamically bind broadcast in activity. When a broadcast is sent, the broadcast actually transmits intent data. The system filters the data based on the <intent-filter> tag content or dynamic intentfilter, onreceive-related operations are performed only when intent data conforms to activity.

You can refer to the specific implementation of broadcast.

Broadcast (http://www.cnblogs.com/qianlifeng/archive/2011/03/06/1972305.html), the author of this blog is very clear, I will not give ugly details.

Next I will briefly talk about how to refresh the front-end interface on the background interface, which is actually a simple combination of service and broadcast.

If broadcast is a static registration, the broadcast file can only be independent. Obviously, it is not what we want. To update the foreground activity interface using the service, we generally use dynamic registration broadcast, register a broadcast in the activity and write an internal class to inherit the broadcast event sent from the intent from broadcastreceive. The Dynamic Registration broadcast function should be removed at the end of the activity lifecycle. For more information, see http://mobile.51cto.com/android-230195.htm.

Register broadcastreceiver

Register a broadcastreceiver in the main program activity to receive the broadcast published by the Service.

  1. @ Override
  2. Protected VoidOnstart () {// override the onstart Method
     
  3. Datareceiver =NewDatareceiver ();
  4. Intentfilter filter =NewIntentfilter (); // create an intentfilter object
     
  5. Filter. addaction ("com. justel. servicebc ");
  6. Registerreceiver (datareceiver, filter); // register broadcast Receiver
     
  7. Super. Onstart ();
  8. }

Revoke broadcastreceiver

Unregisterreceiver (datareceiver); // cancel registering a broadcast Receiver

 

We recommend a blog post:

Broadcastreceiver in Android http://yangguangfu.iteye.com/blog/1063732

 

References:

(1) http://www.cnblogs.com/qianlifeng/archive/2011/03/06/1972305.html

(2) http://blog.csdn.net/yonghumingshishenme/archive/2011/03/23/6271950.aspx

(3) http://mobile.51cto.com/android-230195.htm

 

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.