Broadcastreceiver
Broadcast, an event delivery mechanism that can be used for event delivery across applications (System level).
When using the broadcast, it is not appropriate to add too much logic or time-consuming (not allowed to open threads in the broadcast) operation, more than 10 seconds, resulting in ANR
1 Broadcast types
1.1 Standard Broadcast
After the broadcast is performed asynchronously, all broadcast receivers receive the broadcast almost at the same time.
1.2 Orderly broadcast
Simultaneous broadcast, only one receiver can receive the broadcast at the same time, after the completion of the logical execution, will be passed to the next recipient, of course, can also interrupt the continuation of the broadcast transmission.
2 How to register
2.1 Static Registration
In Androidmanifest.xml, the Declaration
<receiver android:name= ". Demoreciever ">
<intent-filter>
<action android:name= "com.intent.action.boot_completed"/>
</intent-filter>
</receiver>
2.2 Dynamic Registration
Use Intentfilter to register in the code:
Myreveiver receiver = new Myreceiver ();
Intentfilter filter = new Intentfilter ();
Filter.addaction ("com.android.action.boot_completed");
Registerreceiver (Receiver,filter);
2.3 Differences
Static registration as a global listening broadcast, as long as the broadcast sent, regardless of whether the apk is turned on, can receive the broadcast.
Dynamic registration can receive a broadcast only after the registration code has been executed.
3 Sending Broadcasts
3.1 Orderly broadcast
Sendbroadcast (Intent);
3.2 Orderly broadcast
Sendorderbroadcast (Intent,null);
3.2.1 Priority Level
The priority value range can be set in Interfilter (or androidmanifest.xml) by setting priorities (SetPriority ()/android:priority= "+"): -1000~1000, The greater the value, the higher the priority.
3.2.2 Interrupt Broadcast
You can call Abortbroadcast () and interrupt an ordered broadcast to continue delivery.
4 Local broadcast
When we developed the APK, the program internal broadcast did not want to be externally received, to avoid security risks, Android provides internal broadcast Localbroadcastmanager to manage the broadcast.
4.1 Usage
Localbroadcastmanager.getinstance (context);
Instance.registerreceiver (Receiver,filter);
Instance.sendbroadcast (Intent);
Instance.unregisterreceiver (receiver);
4.2 Precautions
<1> local broadcasts cannot be received by static registration.
<2> opening ACTIVITY on the radio requires adding flags:flag_activity_new_task to the intent
<3> broadcast pop-up Alertdialog, need to Set dialog box type is Type_system_alert, so need to join right < uses-permission android:name= " Android.permission.SYSTEM_ALERT_WINDOW "/>
5 Android4.3 above broadcast processing
android4.3 later version, allow the APK to install on the SD card, and then mount the SD, so the APK exists to receive boot_completed broadcast,
In addition, the phone may not have an SD card, so static registration when the broadcast, you need to write two <intent-filter>
<recevier android:name= ". Myrecevier ">
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name= "Android. INTENT. ACTION. Mounted "/>
<action android:name= "Android. INTENT. ACTION. Unmounted "/>
</intent-filter>
</recevier>
6 System Broadcast
Intent.Action_airplane_mode_changed;Broadcast when airplane mode is turned off or turned on<strong>Intent.Action_battery_changed;Charging status, or the battery's power is changedBattery charge status, charge level change, cannot receive this broadcast through the formation statement, only through Context.registerreceiver () registration<strong>Intent.Action_battery_low;Indicates low battery power<strong>Intent.Action_battery_okay;Indicates that the battery is fully charged, that is, it emits a broadcast when it changes from low battery level to full powerIntent.action_boot_completed;Once the system is started, this action is broadcast once (only once).Intent.Action_camera_button;Broadcast when you press the camera button (hardware key) while taking a photoIntent.Action_close_system_dialogs;When the screen times out for the lock screen, the Android system broadcasts the action message when the user presses the power button, long press or short press (whether or not to jump out of the box).Intent.Action_configuration_changed;Broadcast (including changes: interface language, device orientation, etc.) issued when the device's current settings are changed, please refer to Configuration.javaIntent.Action_date_changed;This broadcast will be issued when the device date has changedIntent.Action_device_storage_low;Broadcast when device memory is low, this broadcast can only be used by the system, other apps are not available?Intent.Action_device_storage_ok;Broadcast when device memory is not enough to be sufficient, this broadcast can only be used by the system, other apps are not available?Intent.Action_dock_event;//Where this broadcast was issued Frameworks\base\services\java\com\android\server\dockobserver.javaIntent.Action_external_applications_available;After the mobile app is complete, the broadcast (mobile means: APP2SD)Intent.Action_external_applications_unavailable;When the app is being moved, the broadcast (move refers to: APP2SD)Intent.action_gtalk_service_connected;Gtalk broadcast when a connection has been establishedIntent.action_gtalk_service_disconnected;Gtalk broadcast when a connection has been disconnectedIntent.Action_headset_plug;Broadcast when the headset is plugged into the headphone portIntent.Action_input_method_changed;Broadcasts that are emitted when changing input methodsIntent.Action_locale_changed;Broadcasts emitted when the device's current locale has changedIntent.Action_manage_package_storage;//Intent.Action_media_bad_removal;SD card is not removed correctly (how to remove SD card correctly: Set--SD card and device memory--Uninstall SD card), but broadcast when the SD card is removedBroadcast: The extended media (expansion card) has been unplugged from the SD card slot, but the mount point has not been lifted (unmount)Intent.Action_media_button;Broadcast when the Media button button is pressed (hardware button) if there is a "media button" buttonIntent.Action_media_checking;When inserting an external storage device, such as an SD card, the system will verify the SD card, and the broadcast is issued at this time.Intent.Action_media_eject;Unplugged a broadcast from an external mass storage device (such as an SD card, or a removable hard drive), and the broadcast will be issued with or without proper uninstallation?Broadcast: The user wants to remove the extended media (unplug the expansion card).Intent.action_media_mounted;Broadcasts issued when an SD card is inserted and properly installed (recognized)Broadcast: The extended media is inserted and is already mounted.Intent.Action_media_nofs;//Intent.Action_media_removed;//external storage device has been removed, whether or not properly uninstalled, will issue this broadcast? Broadcast: The extension media is removed. intent.action_media_scanner_finished;//broadcast: A directory that has been scanned for media intent.action_media_scanner_scan_file;//intent.action_ media_scanner_started;//Broadcast: Start scanning a directory of media intent.action_media_shared;//broadcast: The Mount of the extended media is lifted (unmount) because it has been shared as a USB mass storage. intent.action_media_unmountable;//intent.action_media_unmounted//broadcast: The extended media exists but is not mounted (mount). Intent.action_new_outgoing_call;intent. action_package_added;//Successful install APK after//broadcast: A new application package is installed on the device. A new app package is already installed on the device, and the data includes the package name (the newly installed package program cannot receive this broadcast) intent.action_package_changed;//an existing application package has changed, including the package name Intent.action_ package_data_cleared;//to clear the broadcast of an application's data (in Settings-app management-Select an app, then point to clear data?) The user has cleared the data for a package, including the package name (The Purge package program cannot receive this broadcast) intent.action_package_install;//triggers a download and completes the installation of the broadcast, such as downloading the application in the electronic market? intent.action_package_removed;//a successful removal of an APK after the broadcast//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;//the broadcast when replacing an existing installation package (whether the app is now installed newer or older than before, this broadcast will be issued?). intent.action_package_restarted;//the user restarts a package, all processes of the package will be killed and all runtime status associated with it should be removed, including the package name (restart package program cannot receive this broadcast)intent.action_power_connected;//broadcast when an external power supply is plugged in intent.action_power_disconnected;//a broadcast that is issued when an external power connection is disconnected Intent.action_ provider_changed;//intent.action_reboot;//broadcast intent.action_screen_off;//screen is turned off when the device is restarted intent.action_screen_on The broadcast intent.action_shutdown;//when the screen is turned off and the broadcast intent.action_timezone_changed;//time zone changes when the system is turned off Intent.action_ Broadcast intent.action_time_tick;//broadcast when time_changed;//time is set: The current time has changed (normal time elapsed). The current time changes, sent every minute, can not be received through the component Declaration, only through the Context.registerreceiver () method to register intent.action_uid_removed;//a user ID has been removed from the system issued by the broadcast intent.action_ums_connected;//a broadcast when a device has entered a USB mass storage state? intent.action_ums_disconnected;//a broadcast when a device has been converted from a USB mass storage state to a normal state? intent.action_user_present;//intent.action_wallpaper_changed;//a broadcast when a device wallpaper has changed
Android Basics-Broadcastreceiver