Overview of broadcastreceiver in Android

Source: Internet
Author: User

In Android, broadcastreceiver is used to dynamically register broastcastreceiver and statically register broastcastreceiver.

1. dynamically register broastcastreceiver.

Define broadcastreceiver:

Private broadcastreceiver bcr1 = new broadcastreceiver () {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stub string action = intent. getaction (); toast. maketext (context, "dynamic:" + action, 1000 ). show ();}};

Register registerreceiver (bcr1, new intentfilter (action_1). The Code is as follows. The key code is to register the code registerreceiver (bcr1, new intentfilter (action_1). Others are simple buttons and bound click events.

 Button btn1,btn2,btn3;   static final String ACTION_1 = "com.example.broadcastreceiverdemo.ACTION_1";   static final String ACTION_2 = "com.example.broadcastreceiverdemo.ACTION_2";   static final String ACTION_3 = "com.example.broadcastreceiverdemo.ACTION_3";  @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn1 = (Button)this.findViewById(R.id.button1);btn1.setOnClickListener(new ClickEvent());btn2 = (Button)this.findViewById(R.id.button2);btn2.setOnClickListener(new ClickEvent());btn3 = (Button)this.findViewById(R.id.button3);btn3.setOnClickListener(new ClickEvent());registerReceiver(bcr1, new IntentFilter(ACTION_1));}

Send Broadcast

  class ClickEvent implements OnClickListener {  @Override  public void onClick(View v) {  // TODO Auto-generated method stub  if(v == btn1)  {  Intent intent = new Intent(ACTION_1);  sendBroadcast(intent);  }  if(v == btn2)  {  Intent intent = new Intent(ACTION_2);  sendBroadcast(intent);  }  if(v == btn3)  {  IntentFilter filter = new IntentFilter();  filter.addAction(Intent.ACTION_BATTERY_LOW);  filter.addAction(ACTION_3);    registerReceiver(bcr2, new IntentFilter(ACTION_3));    Intent intent = new Intent(ACTION_3);  intent.putExtra("Conuntry", "China");  intent.putExtra("City", "ShangHai");  sendBroadcast(intent);  }  }

V = btn1 is the broadcastreceiver that is sent to the internal dynamic registration;

V = btn2 is the broadcastreceiver sent to the internal static registration;

V = btn3 dynamically registers broadcastreceiver to the system-level internal registration (Bt1 and bt3 are basically the same, but addaction and intent putextra are slightly added );

 

2Static registration of broastcastreceiver. Static registration is more annoying. You must register in androidmanifest. xml.

    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <receiver android:name="cls2">            <intent-filter>                <action android:name="com.example.broadcastreceiverdemo.ACTION_2"></action>            </intent-filter>        </receiver>        <activity            android:name="com.example.broadcastreceiverdemo.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>

And implement a class to inherit broadcastreceiver

Public class cls2 extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stubstring action = intent. getaction (); toast. maketext (context, "static:" + action, 1000 ). show ();}}

 

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.