Android _ Reading Notes _ 3

Source: Internet
Author: User

Simple broadcast and (Intent) reception.

Broadcast is used in many places in Android, and broadcast lecture Information is transmitted to other applications in the operating system. Many device status broadcasts are also generated ...... alas, forget it, hey

I. Broadcast

The Android framework supports two types of broadcast: normal broadcast and ordered boradcast ),

I> the former sends asynchronous messages to all receivers in no specific order, while the latter delivers broadcast events in sequence, the receiver can pass the event to the next receiver in the queue or terminate it.

Ii> both broadcast types have "stickiness", that is, broadcast-related Intent still exists after broadcast is complete, so the broadcast receiver can use registerReceiver () obtain valid Intent data.

1. Send broadcast:

I> Context. sendBroadcast () sends a normal broadcast

Ii> Context. sendOrderedBroadcast () sends ordered Broadcast

Iii> Context. sendStickyOrderded () sends a sticky Broadcast

2. Create a broadcast:

Private static String RECEIVER = "xxx"; // you specify a unique tag

Intent intent = new Intent (receiver er );

SendBroadcast (intent );


The Android operating system processes broadcast transfer. In addition to some broadcasts that are protected or require certain permission to be created, normal broadcasts can be created by any application.

 

Ii. Receive Broadcast

The Android operating system uses the Intent filter to match the broadcast with the appropriate application. To enable the program to become a broadcast receiver:

1> register and receive the broadcast. A specific Intent filter is used by the operating system to match the received broadcast.

2> implement a broadcast receiver class (BroadcastReceiver class) and implement event processing through its onReceiver () callback method.

Note: The implemented broadcast receiver class is an internal class (inner calss), so it should be declared as public static. // I don't quite understand this. If anyone understands this, please tell me.

 

For receiving registered broadcasts:

1> Dynamic Registration: The Contex. registerReceiver () method and the unregisterReceiver () method are used. Dynamic Registration allows the application to disable listening for broadcasts that cannot or do not need to be processed. Generally, the application can register in onResume () of the Activity and register in onPause () cancel (stop listening) in the callback method ).

2> static registration: add the <extends ER> flag to the <application> tag in the Manifest file.

 

A simple example:

1 package com. qimu. intent; 2 3 import android. app. activity; 4 import android. content. broadcastReceiver; 5 import android. content. context; 6 import android. content. intent; 7 import android. content. intentFilter; 8 import android. OS. bundle; 9 import android. view. view; 10 import android. widget. button; 11 import android. widget. textView; 12 13 public class MainActivity extends Activity {14 15 private TextView TV _broad; 16 private Button bt_broad; 17 myBroadcastReceiver receiver; 18 19 private static String QIMU_RECEIVER = "xxx "; 20 21 @ Override22 protected void onCreate (Bundle savedInstanceState) {23 super. onCreate (savedInstanceState); 24 setContentView (R. layout. activity_main); 25 26 TV _broad = (TextView) findViewById (R. id. TV _broad); 27 bt_broad = (Button) findViewById (R. id. bt_broad); 28 29 bt_broad.setOnClickListener (new View. onClickListener () {30 31 @ Override32 public void onClick (View arg0) {33 34 Intent intent = new Intent (QIMU_RECEIVER); 35 intent. putExtra ("broad", "Send Sucess ...... "); 36 sendBroadcast (intent); 37} 38}); 39} 40 41 protected void onResume () {42 super. onResume (); 43 44 faster ER = new myBroadcastReceiver (); 45 IntentFilter filter = new IntentFilter (QIMU_RECEIVER); 46 registerReceiver (filter); 47} 48 49 protected void onPause () {50 super. onPause (); 51 52 unregisterReceiver (receiver ER); 53} 54 public class myBroadcastReceiver extends BroadcastReceiver {56 57 @ Override58 public void onReceive (Context context, Intent intent) {59 60 String _ str = intent. getStringExtra ("broad"); 61 TV _broad.setText (_ str); 62} 63} 64}Intent Broadcast

 

The broadcast of the Processing System: too much, only thanks to the good people, hey, http://blog.chinaunix.net/uid-25370280-id-1735613.html

 

End-of-term review, ah, come on ......

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.