Two broadcast registration methods in android

Source: Internet
Author: User

Two broadcast registration methods in android
BroadcastReceiver, as one of the four major Android components, has a very high role rate, especially when a lot of data is sent through broadcast, such as obtaining text message content and cell phone power. There are usually two Broadcast registration methods:

1) the broadcast is a very resident broadcast. the broadcast is registered in the Activity code, and the broadcast follows the lifecycle of the activity. The broadcast receiver should be removed before the activity where the broadcast is located ends.

2) resident, registration broadcast is passed in the AndroidManfiest. xml file Node registration. When the application is closed, if information is broadcast, it will be automatically run by the system call.

The following are two Broadcast examples:

------------------- Non-resident broadcast, mobile phone power monitoring ----------------------------------------------------

Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">


Android: id = "@ + id/electric_btn"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "view power"/>


Android: id = "@ + id/register_btn"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "get registered broadcast"/>


 

Public class MainActivity extends Activity implements OnClickListener {
/** Display current power */
Private Button policicbtn;
/** Obtain dynamically registered broadcasts */
Private Button getRegisterBtn;


@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
FindViewsById ();
}


Private void findViewsById (){
Required icbtn = (Button) findViewById (R. id. Required ic_btn );
GetRegisterBtn = (Button) findViewById (R. id. register_btn );
ElectricBtn. setOnClickListener (this );
GetRegisterBtn. setOnClickListener (this );
}


@ Override
Public void onClick (View v ){
/** Dynamically register the power monitoring broadcast in the Code */
If (v = policicbtn ){
RegisterReceiver (new ElectricBroadcaseReceiver (), new IntentFilter (Intent. ACTION_BATTERY_CHANGED ));


} Else if (v = getRegisterBtn ){
/** Application package management class */
PackageManager packageManager = this. getPackageManager ();
Intent intent = new Intent ();
Intent. setAction ("android. intent. action. PHONE_STATE ");
List List = packageManager. queryBroadcastReceivers (intent, PackageManager. GET_INTENT_FILTERS );
// Broadcast data information is in list !!!
}
}
}

The corresponding broadcast is:

Public class ElectricBroadcaseReceiver extends BroadcastReceiver {


@ Override
Public void onReceive (Context context, Intent intent ){
/** Receive the broadcast when the battery changes */
If (Intent. ACTION_BATTERY_CHANGED.equals (intent. getAction ())){
/** View power usage */
Int level = intent. getIntExtra ("level", 0 );
/* Electric value */
Int scala = intent. getIntExtra ("scala", 100 );
Toast. makeText (context, "current power:" + level * 100/scala + "%", Toast. LENGTH_LONG). show ();
}
}
}

------------------ Resident broadcast, monitoring SMS ----------------------------------------------

Public class SmsBroadcaseReceiver extends BroadcastReceiver {


@ Override
Public void onReceive (Context context, Intent intent ){
Bundle bundle = intent. getExtras ();
/** Text message content */
Object [] data = (Object []) bundle. get ("pdus ");


/** Message array */
SmsMessage [] messages = new SmsMessage [data. length];
For (int I = 0; I <messages. length; I ++ ){
/** Set the message body content */
Messages [I] = SmsMessage. createFromPdu (byte []) data [I]);
/** Phone number */
String smsnumber = messages [I]. getDisplayOriginatingAddress ();
/** Short Message content */
String smsbody = messages [I]. getDisplayMessageBody ();
}

}

}

Register SmsBroadcaseReceiver broadcast in AndroidManifest. xml:





 

In addition, we will briefly introduce Normal Broadcast and Ordered Broadcast ):

 

Normal broadcast is completely asynchronous for multiple receivers. Generally, each receiver can receive the broadcast without waiting. The receiver does not affect each other. For such broadcast, the receiver cannot terminate the broadcast, that is, it cannot block the receiving actions of other receivers.

Ordered broadcast is special. It is only sent to the receiver with a higher priority each time, and then the receiver with a higher priority spreads to the receiver with a lower priority. The receiver with a higher priority can terminate the broadcast.


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.