Android improvements Broadcastreceiver Examples of detailed _android

Source: Internet
Author: User

The previous articles discussed activity and service separately, and this article discusses broastcastreceiver,broastcast as a means of communication between applications. Broastcastreceiver is also closely linked with intent, dynamic/static registered Broastcastreceiver, after using Sendbroadcast to send intent, The system will automatically start the eligible Broastcastreceiver, which is similar to the interrupt for the embedded system.

The example code shown in this article mainly demonstrates how to register broastcastreceiver statically/dynamically, request power information from the system, and enumerate the fields of information.

The screenshot of the program running is as follows:

The image above is a broadcastreceiver that sends intent to the internal dynamic registration and then displays the message name after receiving it. Dynamic registration Broadcastreceiver used to Registerreceiver ().

The image above is a broadcastreceiver that sends intent to the internal static registration and then displays the message name after receiving it. Static registration is more troublesome than dynamic registration, first create a new class inherits Broadcastreceiver, and then add to Androidmanifest.xml

<receiver android:name= "ClsReceiver2" >
 <intent-filter>
 <action
  android:name= " Com.testBroadcastReceiver.Internal_2 "/>
 </intent-filter>
</receiver>

The first name is the class name and the second is the name of the action.

The image above is an enumeration of intent message fields, this function is more suitable for lazy people, the intent message to the field of all decomposition, and then see which one needs, lazy to remember. The code to implement this section is as follows:

When an unknown intent contains content, you need to enumerate
Bundle B=intent.getextras () by the following methods;
Object[] Lstname=b.keyset (). ToArray ();

for (int i=0;i<lstname.length;i++)
{
 String keyname=lstname[i].tostring ();
 LOG.E (Keyname,string.valueof (B.get (KeyName));
}

The code for Main.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:orientation=" vertical "android:layout_width=" fill_parent "
 android:layout_height=" Fill_parent ">

 <button android:id=" @+id/button01 android:layout_width= "Wrap_content"
 android: layout_height= "Wrap_content" android:text= "sent to internal dynamically registered Broadcastreceiver" ></Button>
 <button Android:id= "@+id/button02" android:layout_width= "wrap_content"
 android:layout_height= "Wrap_content" Android: text= "Send to internal static registration Broadcastreceiver" ></Button>
 <button android:id= "@+id/button03" Android:layout_ Width= "Wrap_content"
 android:layout_height= "wrap_content" android:text= "Send to System Broadcastreceiver" ></ Button>
</LinearLayout>

The code for Testbroadcastreceiver.java is as follows:

Package com.testbroadcastreceiver;
Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;

Import Android.widget.Toast;
  public class Testbroadcastreceiver extends activity {Button Btninternal1,btninternal2,btnsystem;
  Static final String intenal_action_1 = "Com.testBroadcastReceiver.Internal_1";
  Static final String intenal_action_2 = "com.testBroadcastReceiver.Internal_2";
  Static final String intenal_action_3 = "Com.testBroadcastReceiver.Internal_3";
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    btninternal1= (Button) This.findviewbyid (R.ID.BUTTON01);
    Btninternal1.setonclicklistener (New Clickevent ());
Btninternal2= (Button) This.findviewbyid (R.ID.BUTTON02);    Btninternal2.setonclicklistener (New Clickevent ());
    Btnsystem= (Button) This.findviewbyid (R.ID.BUTTON03);
    Btnsystem.setonclicklistener (New Clickevent ());
  Dynamic registration Broadcast Message Registerreceiver (BCRINTENAL1, New Intentfilter (intenal_action_1)); Class Clickevent implements view.onclicklistener{@Override public void OnClick (View v) {if (V==BTNINTERNAL1)//To be moved
  State registered Broadcastreceiver send data {Intent Intent = new Intent (intenal_action_1);
  Sendbroadcast (Intent);
  else if (V==BTNINTERNAL2)//Send data to the statically registered Broadcastreceiver {Intent Intent = new Intent (intenal_action_2);
  Sendbroadcast (Intent); else if (v==btnsystem)//dynamic register to receive 2 sets of information Broadcastreceiver {intentfilter filter = new Intentfilter ();//Filter.add Action (intent.action_battery_changed);//system battery Detection information filter.addaction (intenal_action_3);//The third group of custom message Registerreceiver (
  
  Batinforeceiver, filter);
  Intent Intent = new Intent (intenal_action_3);
  Intent.putextra ("Name", "HELLOGV"); Intent.putextra ("Blog", "http:BLOG.CSDN.NET/HELLOGV "); Sendbroadcast (intent);//Pass Past}}/* * Receive dynamically registered broadcast Broadcastreceiver/private Broadcastreceiver Bcrinten Al1 = new Broadcastreceiver () {public void OnReceive (context context, Intent Intent) {String action = Intent.getact
  Ion ();
 Toast.maketext (Context, "Dynamic:" +action, 1000). Show ();
 }
 }; Private Broadcastreceiver Batinforeceiver = new Broadcastreceiver () {public void OnReceive (context context, Intent Inten
  T) {String action = intent.getaction (); If the action captured is action_battery_changed if (Intent.ACTION_BATTERY_CHANGED.equals action) {//when the unknown Intent contains the content,
  It is necessary to enumerate Bundle B=intent.getextras () by the following methods;

  Object[] Lstname=b.keyset (). ToArray ();
   for (int i=0;i<lstname.length;i++) {String keyname=lstname[i].tostring ();
  LOG.E (Keyname,string.valueof (B.get (KeyName))); }//If the action captured is intenal_action_3 if (intenal_action_3.equals action) {//When the unknown intent contains content, you need to enumerate the following methods Bundle
  B=intent.getextras (); Object[] LSTName=b.keyset (). ToArray ();
   for (int i=0;i<lstname.length;i++) {String keyname=lstname[i].tostring ();
  LOG.E (keyname,b.getstring (KeyName));
}
  }
 }
 };

 }

The code for Clsreceiver2.java is as follows:

Package com.testbroadcastreceiver;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.widget.Toast;
*
 * receive static registered broadcast of Broadcastreceiver,
 * Step1: to androidmanifest.xml here to register messages
 *  <receiver android:name= "ClsReceiver2" >
  <intent-filter>
  <action
   android:name= " Com.testBroadcastReceiver.Internal_2 "/>
  </intent-filter>
 </receiver>
 step2: The string step3 that defines the message
 : Intent the message to drive the Broadcastreceiver trigger/public
class ClsReceiver2 extends broadcastreceiver{
 @Override public
 void OnReceive (context context, Intent Intent) {
 String action = Intent.getaction ();
 Toast.maketext (Context, "Static:" +action, 1000). Show ();


Interested friends can debug running this instance, hoping to be able to play a little help for everyone's Android project development.

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.