Android Broadcast Mechanism

Source: Internet
Author: User
Android Broadcast Mechanism

Method 1: static registration

Step 1:

Mybroadcastreceiver extends broadcastreceiver

Override the onreceive (context CT, intent) method to complete the work you want.

Step 2:

 

Register the Declaration in androidmanifest. xml:

 

<Cycler Android: Name = ". ringermodechangedreceiver">
<Intent-filter>
<Action Android: Name = "android. Media. ringer_mode_changed"/>
</Intent-filter>
</Cycler>

 

Method 2: Dynamic Registration

Write two methods in myactivity:

For example, register a broadcast receiver when myactivity is started and cancel the broadcast receiver when myactivity is destroyed.

Check the Code:

Public void oncreate (bundle ){

 

Registerreceiver (mybroadcastreceiver, new intentfilter ("custom or system string "));

 

}

 

Public void ondestory (){

 

Unregisterreceiver (mybroadcastrecevier );

 

}

(Static registration of broadcast is not allowed for some events, such as checking the remaining battery of the cell phone)

 

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.