Android Learning Note (17) Broadcastreceiver

Source: Internet
Author: User

1. Receiving broadcasts
    1. Create a class that inherits Broadcastreceiver, and the OnReceive () method in which it is replicated
    2. Register the Broadcastreceiver in the Androidmanifest.xml file

After the setting is complete, if there is a broadcast that meets the criteria, the system automatically generates a corresponding Broadcastreceiver object, and then calls the OnReceive () method inside. When the method is executed, the Boadcastreceiver object is destroyed. The next time the trigger is generated is a new Broadcastreceiver object.

 public  class  myreceiver extends   broadcastreceiver{@Override  public  void   OnReceive (Context Context, Intent Intent) { // dosomething
    }}  
<android:name= ". Myreceiver ">    <intent-filter>         <   android:name= "Android.provider.Telephony.SMS_RECEIVED"/>     </intent-filter></Receiver>
2. Send Broadcast
    1. Create a intent to send
    2. Send broadcast via Sendbroadcast () or Sendorderedbroadcast ()
 Public void OnClick (View v) {    // Create Intent object    new  Intent ();    Intent.setaction ("Android.provider.Telephony.SMS_RECEIVED");    Intent.putextra ("msg", "message");     // Send broadcast     Sendbroadcast (intent);}

Android Learning Note (17) Broadcastreceiver

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.