Android app-Explanation of Android Broadcast mechanism Broadcast

Source: Internet
Author: User

Android app-Explanation of Android Broadcast mechanism Broadcast
In Android, after some operations are completed, a broadcast will be sent, for example, a text message or a phone call. If a program receives the broadcast, the corresponding processing will be performed. This broadcast is somewhat similar to the radio broadcast in our traditional sense. It is called Broadcast because it is only responsible for "speaking", regardless of "listening or listening", that is, no matter how the receiver handles it. In addition, broadcast can be received by more than one application, and of course it may not be received by any application. I. Three elements of the Android Broadcast mechanism: 1. Broadcast: used to send broadcasts. It is a widely used mechanism for transmitting information between applications. 2. BroadcastReceiver is used to receive broadcasts. It is a component that filters and accepts and responds to the Broadcast. 3. Intent content (Intent): media used to store broadcast-related information 2. Broadcast functions and features: 1. the broadcast life cycle is short, the entire process is completed after the onReceive object is called. From the perspective of implementation complexity and amount of code, broadcast is undoubtedly the most mini Android component, and the Implementation usually requires only a few lines of code. After a broadcast object is constructed, only the BroadcastReceiver. onReceive method is executed to end its lifecycle. So sometimes we can regard it as a function. 2. Like all components, broadcast objects are constructed in the main thread of the application process, so the execution of broadcast objects must be synchronized and fast. It is not recommended to open sub-threads in it, because the broadcast object has been executed and destroyed by the system. If you need to complete a time-consuming task, you should send Intent to the Service, which is done by the Service. 3. Each time the broadcast arrives, the BroadcastReceiver object is re-created and the onReceive () method is called. After execution, the object is destroyed. when the onReceive () method is not completed within 10 seconds, Android considers the program to be unresponsive. 3. Two registration methods for broadcast: 1. Resident broadcast: Resident broadcast. If your application is closed, the broadcast receiver you write can receive the same message. Its registration method is in AndroidManifast of your application. xml, which is also called static registration. This method can be understood as the broadcast registered through the list file is handed over to the operating system for processing. The sample code is as follows: copy the Code <er android: name = ". AlarmReceiver"> <! -- Reveiver name. If it is an internal static registration broadcast, add $ --> <intent-filter> <action android: name = "android. intent. action. ALARM_RECEIVER "/> <! -- Intent of broadcast receipt --> <category android: name = "android. intent. category. DEFAULT "/> </intent-filter> </javaser> copy the code to create an AlarmReceiver. inherit from BroadcastReceiver to copy the public class AlarmReceiver extends BroadcastReceiver {@ Override public void onReceive (Context arg0, intent arg1) {Toast. makeText (arg0, "I'm an alarm clock. I want to wake you up... ", Toast. LENGTH_SHORT ). show () ;}} copy the code and create a simple Activity. Then, send a static broadcast to copy code 1 package com. exa Mple. alarmmanager; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. OS. bundle; 6 7 public class MainActivity extends Activity {8 9 @ Override10 protected void onCreate (Bundle savedInstanceState) {11 super. onCreate (savedInstanceState); 12 setContentView (R. layout. activity_main); 13 14 // send a static broadcast 15 Intent intent = new Intent ("android. intent. action. ALARM_RECEIVER "); 16 SendBroadcast (intent); 17} 18 19} copy Code 2, very resident broadcast: Very resident broadcast, when the application is over, the broadcast will naturally be gone, for example, register the broadcast receiver in onCreate or onResume in the Activity and cancel the broadcast receiver in onDestory. In this way, your broadcast receiver is very resident. This registration method is also called Dynamic Registration. This method can be understood as the code-registered broadcast is associated with the registrant. For example, write a broadcast receiver that listens to the SDcard status: Copy code 1 package com. example. alarmmanager; 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. OS. environment; 10 11 public class MainActivity extends Activity {12 SdcardStateChanageReceiver sdcardStateR Eceiver; 13 14 protected void onCreate (Bundle savedInstanceState) {15 super. onCreate (savedInstanceState); 16 17 // create a new BroadcastReceiver18 sdcardStateReceiver = new SdcardStateChanageReceiver (); 19 // Add intent filter 20 IntentFilter filter = new IntentFilter (); 21 // Add an Action to the filter, indicating that only the 22 filters of these actions are received. addAction (Intent. ACTION_MEDIA_REMOVED); 23 filter. addAction (Intent. ACTION_MEDIA_EJECT); 24 filter. addAct Ion (Intent. ACTION_MEDIA_MOUNTED); 25 // set the SDCard plugging and receiving 26 filters. addDataScheme ("file"); 27 // register a dynamic broadcast 28 registerReceiver (sdcardStateReceiver, filter); 29} 30 31 protected void onDestroy () {32 // remember to log out of broadcast when destroying the Activity. Otherwise, the error 33 unregisterReceiver (sdcardStateReceiver) will be reported ); 34} 35 36 // BroadcastReceiver37 class SdcardStateChanageReceiver extends BroadcastReceiver {38 public void onReceive (Context context, In Tent intent) {39 String state = Environment. getExternalStorageState (); 40 System. out. println (state); 41 if (state. equals (Environment. MEDIA_REMOVED) 42 | state. equals (Environment. MEDIA_UNMOUNTED) {43 System. out. println ("SDCard has been uninstalled! "); 44} 45} 46} 47}

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.