BroadcastReceiver-Android broadcast mechanism, broadcastreceiver

Source: Internet
Author: User

BroadcastReceiver-Android broadcast mechanism, broadcastreceiver
What is broadcast?

Television channels, radios, cell phones, and special broadcasts in life all have their own specific broadcasts. No matter whether someone cares or listens, whether you watch TV or not, each channel implements playing based on its own progress, and the radio is also! So the android broadcast mechanism I understand is like this-the broadcast publisher is only responsible for sending events, and does not care about whether there are listeners or listeners who receive the events.

Broadcast usage in androidFrom the above introduction, it is not difficult to find that it is used to transmit data. The details are as follows: How to broadcast

Now let's implement a simple broadcast program. Android provides two formats for registering a broadcast receiver: Dynamic registration in a program and specified in xml. The difference between them is that the scope of the function is different. The receiver for Dynamic Registration of the program is only valid during the program running process, and the receiver for xml registration will take effect no matter whether your program is started or not. First, we will introduce the Dynamic Registration Method in the program.

Android has a system broadcast and can also customize broadcast. To Accept Broadcast information, we have to implement this broadcast receiver by ourselves. We can inherit BroadcastReceiver to have a broadcast receiver. There is not enough receiver. We have to rewrite the onReceiver method in BroadcastReceiver. What should we do when we come to broadcast? The following is a small program to demonstrate the broadcast application.

I. Registration (when a broadcast receiver is implemented, you must also set the type of information that the broadcast receiver receives. Here is the information: android. provider. Telephony. SMS_RECEIVED)

  • Configure broadcast in AndroidManifest. xml
  •  

     

    2. inherit from BroadcastReceiver and override the onReceiver method. Here we listen to the text message sending, which will trigger this broadcast, parse the text message content and display it:
    12345678910111213141516171819 public class SmsBroadCastReceiver extends BroadcastReceiver   {         @Override     public void onReceive(Context context, Intent intent)      {          Bundle bundle = intent.getExtras();          Object[] object = (Object[])bundle.get("pdus");          SmsMessage sms[]=new SmsMessage[object.length];          for(int i=0;i<object.length;i++)          {              sms[0] = SmsMessage.createFromPdu((byte[])object[i]);              Toast.makeText(context, "From"+sms[i].getDisplayOriginatingAddress()+"The message is :"+sms[i].getDisplayMessageBody(), Toast.LENGTH_SHORT).show();          }          // Terminate the broadcast. Here we can handle it a little, and implement the SMS Firewall Based on the Number entered by the user.        abortBroadcast();      }          

      

    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.