Android broadcast BroadcastReceiver

Source: Internet
Author: User

Android broadcast BroadcastReceiver
Android broadcast BroadcastReceiver

The Android system defines a variety of broadcasts, such as battery usage, telephone reception, and SMS reception. A broadcast is generated when the device is started. Of course, you can also customize your own broadcast.

When it comes to broadcast, there must be a Broadcast Sender and broadcast receiver. The sender of the System broadcast is the system, and the custom broadcast is of course user-defined.

We can define a broadcast receiver to receive broadcasts we are interested in, whether it is system broadcast or custom broadcast. This broadcast receiver must inherit from BroadcastReceiver.

Old Rules: Let's start with basic knowledge.

I. Basic Knowledge 1: Define a broadcast receiver that inherits BroadcastReceiver. 2: In the broadcast receiver, onReceive receives and processes the broadcast Action. Different broadcasts have different actions. 3: register the broadcast in the manifest. xml file, or use the registerReceiver method to register the broadcast. Of course, unregisterReceiver can delete the broadcast. The following shows the broadcast defined by the system, that is, the ActionView Code of the System broadcast.
Intent. ACTION_AIRPLANE_MODE_CHANGED; // broadcast Intent when flight mode is disabled or enabled. ACTION_BATTERY_CHANGED; // The charging status, or the battery power change // The battery charging status and charge level change. You cannot receive this broadcast through the formation Declaration, only through Context. registerReceiver () registers Intent. ACTION_BATTERY_LOW; // indicates that the battery power is low. Intent. ACTION_BATTERY_OKAY; // indicates that the battery is sufficient, that is, Intent is broadcast when the battery power changes from low to full. ACTION_BOOT_COMPLETED; // after the system is started, this action is broadcast once (only once ). Intent. ACTION_CAMERA_BUTTON; // broadcast Intent that is triggered when you press the camera button (hardware button. ACTION_CLOSE_SYSTEM_DIALOGS; // when the screen times out to lock the screen, when the user presses the power button, long or short press (no matter whether the dialog box exists), to lock the screen, the android System Broadcasts the Intent message of this Action. ACTION_CONFIGURATION_CHANGED; // The broadcast that is triggered when the current device settings are changed (For details, refer to Configuration. java) Intent. ACTION_DATE_CHANGED; // This broadcast Intent is triggered when the device date changes. ACTION_DEVICE_STORAGE_LOW; // broadcast triggered when the device memory is insufficient. This broadcast can only be used by the system, and other apps are unavailable? Intent. ACTION_DEVICE_STORAGE_ OK; // broadcast sent when the device memory is not full enough. This broadcast can only be used by the system, and other apps are unavailable? Intent. ACTION_DOCK_EVENT; // The place where the broadcast is sent: frameworks \ base \ services \ java \ com \ android \ server \ DockObserver. javaIntent. ACTION_EXTERNAL_APPLICATIONS_AVAILABLE; // broadcast (mobile refers to APP2SD) Intent sent after the mobile APP is completed. ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE; // The broadcast (mobile refers to APP2SD) Intent when the APP is being moved. ACTION_GTALK_SERVICE_CONNECTED; // broadcast Intent sent when a connection is established in Gtalk. ACTION_GTALK_SERVICE_DISCONNECTED; // broadcast Intent sent when Gtalk is disconnected. ACTION_HEADSET_PLUG ;/ /The broadcast Intent sent when the headset is inserted in the headphone port. ACTION_INPUT_METHOD_CHANGED; // broadcast Intent sent when the input method is changed. ACTION_LOCALE_CHANGED; // specifies the broadcast Intent that is sent when the current region of the device is changed. ACTION_MANAGE_PACKAGE_STORAGE; // Intent. ACTION_MEDIA_BAD_REMOVAL; // the SD card is not removed correctly (the SD card is removed correctly by setting -- SD card and device memory -- detaching the SD card), but the broadcast/broadcast sent when the SD card is removed: the expansion media (expansion card) has been removed from the SD card slot, but the mount point has not been removed (unmount) Intent. ACTION_MEDIA_BUTTON; // broadcast when you press the "Media Button" Button. If there is a "Media Button" Button (hardware Button), Intent. ACTION_M EDIA_CHECKING; // insert an external storage device. For example, when an SD card is installed, the system checks the SD card? Intent. ACTION_MEDIA_EJECT; // The broadcast sent by the external large-capacity storage device (such as the SD card or mobile hard disk) has been unmounted. Will this broadcast be sent whether or not it is correctly uninstalled? // Broadcast: You want to remove the extended media (unplug the expansion card ). Intent. ACTION_MEDIA_MOUNTED; // broadcast sent when the SD card is inserted and correctly installed (identified): the extended media is inserted and mounted. Intent. ACTION_MEDIA_NOFS; // Intent. ACTION_MEDIA_REMOVED; // The external storage device has been removed. Will this broadcast be sent no matter whether it is uninstalled correctly? // Broadcast: the extended media is removed. Intent. ACTION_MEDIA_SCANNER_FINISHED; // broadcast: A Media Directory Intent has been scanned. ACTION_MEDIA_SCANNER_SCAN_FILE; // Intent. ACTION_MEDIA_SCANNER_STARTED; // broadcast: Start to scan a Media Directory Intent. ACTION_MEDIA_SHARED; // broadcast: the mounting of the extended media is unmounted because it has been shared as a USB large capacity storage. Intent. ACTION_MEDIA_UNMOUNTABLE; // Intent. ACTION_MEDIA_UNMOUNTED // broadcast: the extended media exists, but has not been mounted ). Intent. ACTION_NEW_OUTGOING_CALL; Intent. ACTION_PACKAGE_ADDED; // After the APK is successfully installed // broadcast: An application package is installed on the device. // A new application package has been installed on the device, and the data includes the package name (the latest package program cannot receive this broadcast) Intent. ACTION_PACKAGE_CHANGED; // an existing application package has been changed, including the package name Intent. ACTION_PACKAGE_DATA_CLEARED; // The broadcast sent when the data of an application is cleared (when setting -- Application Management -- select an application and then click "Clear Data ?) // The user has cleared the data of a package, including the package name (the package clearing program cannot receive this broadcast) Intent. ACTION_PACKAGE_INSTALL; // a broadcast triggered when the download is completed, such as downloading an application in the e-marketplace? // Intent. ACTION_PACKAGE_REMOVED; // broadcast sent after an APK is deleted successfully // an existing application package has been removed from the device, including the package name (the package program being installed cannot receive this broadcast) Intent. ACTION_PACKAGE_REPLACED; // broadcast sent when an existing installation package is replaced (whether the installed APP is newer or older than the previous one ?) Intent. ACTION_PACKAGE_RESTARTED; // when the user starts a new package, all processes in the package will be killed, and all running time statuses associated with the package should be removed, including the package name (restarting the package program cannot receive this broadcast) Intent. ACTION_POWER_CONNECTED; // broadcast Intent sent when the external power is plugged in. ACTION_POWER_DISCONNECTED; // broadcast Intent that is triggered when the external power is disconnected. ACTION_PROVIDER_CHANGED; // Intent. ACTION_REBOOT; // broadcast Intent when the device is restarted. ACTION_SCREEN_OFF; // broadcast Intent after the screen is disabled. ACTION_SCREEN_ON; // broadcast Intent after the screen is opened. ACTION_SHUTDOWN; // broadcast Intent sent when the system is shut down. ACTION_TIMEZONE_CHANGED; // time zone changed When the broadcast Intent. ACTION_TIME_CHANGED; // broadcast Intent. ACTION_TIME_TICK; when the time is set; // broadcast: the current time has changed (normal time elapsed ). // The current time changes and is sent every minute. It cannot be received through the component Declaration, only through Context. registerReceiver () method to register Intent. ACTION_UID_REMOVED; // a user ID has removed the broadcast from the system. // Intent. ACTION_UMS_CONNECTED; // is the broadcast sent when the device enters the USB high-capacity storage status? Intent. ACTION_UMS_DISCONNECTED; // broadcast when the device has changed from the USB bulk storage status to normal status? Intent. ACTION_USER_PRESENT; // broadcast sent when the Intent. ACTION_WALLPAPER_CHANGED; // device wallpaper has changed
2. In practice, we listen to the text message and get the prompt when the text message content is hello. In addition, we listen to the custom broadcast and respond. The Code is as follows: View Code
Package com. dongzi; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. bundle; import android. telephony. smsMessage; import android. widget. toast;/*** listen to SMS information broadcast and MSG custom broadcast * @ author **/public class SMSReceiver extends BroadcastReceiver {// System broadcast, custom broadcast static final String SMS_ACTION = "android. provider. telephony. SMS_RECEIVED "; static fin Al String MSG_ACTION = "com. dongzi. customMsg "; static final String HELLO =" hello "; static final String BUNDLE =" bundle "; String receiveMsg =" "; @ Override public void onReceive (Context context, Intent intent) {SmsMessage [] msg = null; Bundle bundle = null; // The System Broadcasts if (intent. getAction (). equals (SMS_ACTION) {bundle = intent. getExtras (); if (bundle! = Null) {Object [] objs = (Object []) bundle. get ("puds"); msg = new SmsMessage [objs. length]; for (int I = 0; I
 
  
Then we can register in the Code, or register in the configuration file // IntentFilter filter = new IntentFilter (); // filter. addAction (SMSReceiver. MSG_ACTION); // registerReceiver (SMSReceiver. class. newInstance (), filter); // unregisterReceiver (receiver)
   
      
    
  They will intercept all broadcasts that match the action, and then let's send broadcasts in the code. View Code
  
// Send custom broadcast private void sendCustomBroadcast () {Intent intent = new Intent (SMSReceiver. MSG_ACTION); Bundle bundle = new Bundle (); bundle. putString (SMSReceiver. HELLO, "hello"); intent. putExtra (SMSReceiver. BUNDLE, bundle); sendBroadcast (intent );}

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.