One of the four Android components broadcast recipients

Source: Internet
Author: User

One of the four components

Broadcast recipient Broadcasetrecevice

The mechanism of information transfer in Android:

Broadcast: One-way, mass

To create a feature:

1. Declare an inheritance broadcasetrecevice

2. Declare in the manifest file

Package name. Class Name

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

To receive a broadcast: To register a broadcast recipient's filter:

There are two forms of registered broadcast recipients:

The first: Register directly in the manifest file:

Example: Receiving SMS

1. Permission issues to receive text messages

<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>


2. Filter conditions for SMS

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/la Ng/zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

3. Modify the contents of the Onrecevice ().

Once the broadcast is received, the current method is triggered

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Note: The method of registering the filter condition of the broadcast receiver in the manifest file has the following characteristics:

Even if the broadcast recipient's app is not executing, as long as the broadcast filter condition is always received and the Onrecevice () method is triggered



The second method of broadcasting registration:

Code:

1. Registration: 650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/1F/wKioL1bgxTyzurSfAAIsjncSDZA640.jpg "title=" 54861030860046b9891b4489e9d03771.jpg "alt=" Wkiol1bgxtyzursfaaisjncsdza640.jpg "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/l Ang/zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

2. Logout

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m02/7d/21/wkiom1bgxnkr563-aafnyiy644o007.jpg "title=" 11111.jpg "alt=" Wkiom1bgxnkr563-aafnyiy644o007.jpg "/>

Note: A broadcast recipient registered in the code can receive it only when the interface is present.

If the interface does not exist, the broadcast will not receive

The difference between manifest file registration and code registration

1. Register the filter in the manifest file: Even if the process does not exist, it can be received as long as the filter matches the broadcast.

2. In the Code Registration filter: The interface exists broadcast to receive. Use is more flexible.

The embodiment of code registration flexibility:

Internal class inheritance can be declared in activity Broadcastreceiver

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Benefits of declaring as internal classes:

When we register our declared broadcast receiver object in this interface, when we receive the broadcast trigger OnReceive () method, in this method we can directly modify the member variable of the activity and call its method.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>


One of the four Android components broadcast recipients

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.