Broadcastreceiver--android Broadcasting mechanism

Source: Internet
Author: User

what is broadcast

Life's TV channels, radios, mobile phones, have their own specific broadcasts, whether or not someone cares, listen to, regardless of whether you watch TV, each channel is implemented according to their own progress to play, Radio is! So that's what I understand about the Android broadcasting mechanism--the broadcast publisher is only responsible for sending out the events that are happening, and it doesn't matter if there is a listener or how the listener receives the deal.

    1. We all know the content of the radio is mostly voice broadcast, and Android in our Broadcastreceiver equivalent to an event, the content is stored in the intent, it to complete the transfer work, As to whether the processing is done by the parameters in the Intent-filter to match the filter to find out what you care about, and then to operate
    2. In the life of the user by adjusting the specific channel, frequency to accept radio, television information. The content of the broadcast in Android is to accept a specific broadcast by registering a broadcast, and only the action that is sent and the action that receives the broadcast are the same, sometimes collectively to the published content, the recipient can accept the broadcast
    3. Release broadcast in Android by Sendbroadcast this method to send
The purpose of broadcasting in AndroidFrom the above introduction is not difficult to find, used to transfer data. Specific as follows:
    1. The data transfer and sharing between different programs can be implemented easily, as long as the sending and receiving are in the same action. such as commonly used to receive text messages, then Android has written a broadcast to deal with, the analysis of the display of the hands of the Inbox, click to show the sender's content, do not want to answer someone's phone or text messages, can use the broadcast to achieve, so the broadcast in the Android application is very extensive.
    2. The main UI can be updated in real time via broadcast, and there are many more ways to update the main UI.
how to achieve broadcasting

Now we're going to implement a simple broadcast program. Android provides two forms of registered broadcast recipients, which are dynamically registered in the program and specified in the XML. The difference between them is that the scope of the action is different, the recipient of the program dynamic registration is only valid while the program is running, and the recipient of the XML registration will work regardless of whether your program has started. The method of dynamic registration in the program is introduced first.

    1. Dynamic Registration : Not a resident broadcast, which means that the broadcast follows the activity's life cycle. Note: Remove the broadcast receiver before the activity ends.
    2. configuration file Mode : Resident XML Manifest registration, that is, when the application is closed, if there is information broadcast, the program will be automatically run by the system call. Whether or not the application is started, the broadcast is monitored in the background as soon as it is powered on.

Android has a system broadcast can also customize the broadcast, to accept the broadcast information, then the broadcast receiver will have to be implemented by ourselves, we can inherit broadcastreceiver, we can have a broadcast receiver. There is a receiver is not enough, we have to rewrite broadcastreceiver inside the Onreceiver method, when to broadcast what we want to do, the following small program to display the broadcast application.

First, register (when the broadcast receiver is implemented, also set the broadcast receiver to receive broadcast information type, here is the information: Android.provider.Telephony.SMS_RECEIVED)

    1. Dynamic Registration method
      1. 1234567 //generate broadcast processing    smsbroadcastreceiver =  new  smsbroadcastreceiver ();    //instantiate the filter and set the broadcast    to be filtered; intentfilter Intentfilter =  new  intentfilter ( "Android.provider.Telephony.SMS_RECEIVED"  //Register broadcast    broadcastreceiveractivity. this .registerreceiver (Smsbroadcastreceiver, intentfilter);  
    2. Configuring broadcasts in Androidmanifest.xml
      1. 1234567891011121314151617181920212223242526272829 <?xml version="1.0"encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="spl.broadCastReceiver"      android:versionCode="1"      android:versionName="1.0"    <application android:icon="@drawable/icon"android:label="@string/app_name"        <activity android:name=".BroadCastReceiverActivity"                  android:label="@string/app_name"            <intent-filter>                 <action android:name="android.intent.action.MAIN"/>                 <category android:name="android.intent.category.LAUNCHER"/>             </intent-filter>         </activity>                    <!--广播注册-->         <receiver android:name=".SmsBroadCastReceiver"            <intent-filter android:priority="20"                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>             </intent-filter>         </receiver>                </application>            <uses-sdk android:minSdkVersion="7" />            <!-- 权限申请 -->     <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>        </manifest>  

Second, inherit Broadcastreceiver, rewrite the Onreceiver method. Here is the message sent by the monitor, will trigger the broadcast, and then the text content to parse out and show:
12345678910111213141516171819 publicclassSmsBroadCastReceiver extendsBroadcastReceiver   {        @Override    publicvoid onReceive(Context context, Intent intent)      {          Bundle bundle = intent.getExtras();          Object[] object = (Object[])bundle.get("pdus");          SmsMessage sms[]=new SmsMessage[object.length];          for(inti=0;i<object.length;i++)          {              sms[0] = SmsMessage.createFromPdu((byte[])object[i]);              Toast.makeText(context, "来自"+sms[i].getDisplayOriginatingAddress()+" 的消息是:"+sms[i].getDisplayMessageBody(), Toast.LENGTH_SHORT).show();          }          //终止广播,在这里我们可以稍微处理,根据用户输入的号码可以实现短信防火墙。          abortBroadcast();      }         

  

      

Broadcastreceiver--android Broadcasting mechanism

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.