Android Broadcast Broadcastreceiver

Source: Internet
Author: User
Tags time zones

The Android system defines a variety of broadcasts, such as the status of the battery, the receipt of the phone, and the receipt of the text message, which generates a broadcast on startup. Of course, users can also customize their own broadcasts.

When it comes to broadcasting, there must be a broadcast sender, and a 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 the broadcasts we are interested in, whether it's a system broadcast or a user-defined broadcast. This broadcast receiver must inherit to Broadcastreceiver.

The usual, first, a little basic knowledge.

A Basic knowledge 1: Define a broadcast receiver, inheriting broadcastreceiver. 2: OnReceive receives the action of the broadcast and makes processing in the broadcast receiver. Different broadcasts are for a different set of action. 3: Register the broadcast in the Manifest.xml file, or register the broadcast through the Registerreceiver method, of course Unregisterreceiver can delete the broadcast. The system-defined broadcast, the action of the system broadcast, is given below
intent.action_airplane_mode_changed;
//Broadcast when airplane mode is turned off or turned on

intent.action_battery_changed;
//Charging status, or the battery's power is changed
//Battery charge status, charge level change, cannot receive this broadcast through the formation statement, only through Context.registerreceiver () registration

Intent.action_battery_low;
//Indicates low battery power

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 power

intent.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 photo

Intent.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.java

intent.action_date_changed;
//This broadcast will be issued when the device date has changed

Intent.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.java

intent.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 established

intent.action_gtalk_service_disconnected;
//Gtalk broadcast when a connection has been disconnected

Intent.action_headset_plug;
//Broadcast when the headset is plugged into the headphone port

intent.action_input_method_changed;
//Broadcasts that are emitted when changing input methods

intent.action_locale_changed;
//Broadcasts emitted when the device's current locale has changed

Intent.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 removed
//Broadcast: 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" button

intent.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;
//The external storage device has been removed, and this broadcast will be issued, whether or not it is uninstalled properly?
//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;
//After the successful installation apk
//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 latest 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;
//Trigger a download and complete the broadcast of the installation, such as downloading the app in the electronics market?
//
intent.action_package_removed;
//Successful deletion 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;
//A broadcast that is issued when an existing installation package is replaced (regardless of 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 plugged in to an external power source
intent.action_power_disconnected;
//Broadcasts issued when an external power connection has been disconnected
intent.action_provider_changed;
//

Intent.action_reboot;
//Broadcast when restarting a device

Intent.action_screen_off;
//Broadcast after the screen is closed

intent.action_screen_on;
//Broadcast after the screen is opened

Intent.action_shutdown;
//Broadcasts issued when the system is shut down

intent.action_timezone_changed;
//Broadcasts issued when time zones change

intent.action_time_changed;
//Broadcasts emitted when time is set

Intent.action_time_tick;
// broadcast: The current time has changed (normal time elapsed).
// The current time is changed, sent every minute, cannot be received by component Declaration, Only the Context.registerreceiver () method is used to register

Intent.action_uid_removed;
// A user ID has been removed from the system issued by the broadcast

intent.action_ums_connected;
// broadcast when a device has entered a USB mass storage state?

intent.action_ums_disconnected;
// broadcast when a device has moved from a USB mass storage state to a normal state?

Intent.action_user_present;
//

intent.action_wallpaper_changed;
// broadcast when device wallpaper has changed
Two Actual combatLet's say we listen for text messages and get the message content as Hello when prompted, and we listen to our custom broadcasts and react. The code is as follows
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 for SMS message broadcasts, and MSG custom broadcasts
*@author
*
*/
PublicClass SmsreceiverExtends Broadcastreceiver {

//System broadcast, custom broadcast
StaticFinal String sms_action= "Android.provider.Telephony.SMS_RECEIVED";
StaticFinal String msg_action= "COM.DONGZI.CUSTOMMSG";
StaticFinal String hello= "HELLO";
StaticFinal String bundle= "BUNDLE";
String receivemsg= "";
@Override
Publicvoid OnReceive (Context context, Intent Intent) {

Smsmessage[] Msg=Null
Bundle bundle=Null
//Listen to the system broadcast here.
if (Intent.getaction (). Equals (Sms_action)) {
Bundle=intent.getextras ();
if (Bundle! =NULL) {
Object[] objs= (object[]) bundle.get ("PUDs");
msg=New Smsmessage[objs.length];
Forint i=0;i<objs.length;i++)
Msg[i]= SMSMESSAGE.CREATEFROMPDU ((Byte[]) objs[i]);

Forint j=0;j<msg.length;j++) {
String Msg_str=msg[j].getmessagebody ();
if (Msg_str.equals (HELLO)) {
Toast.maketext (context, MSG_STR, Toast.length_long). Show ();
Return
}else{
Toast.maketext (context, MSG_STR, Toast.length_long). Show ();
return;
}
}
}

}// Listen to our custom broadcasts here
Else if (intent.getaction (). Equals (Msg_action)) {
Bundle=intent.getbundleextra (BUNDLE);
if (bundle! = null) {
String hello=bundle.getstring (hello);
if (hello!=null &&hello.equals (hello)) {
Toast.maketext (context, Hello, Toast.length_long). Show ();
return;
}
}
}

}

}
then we can either 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)<receiver android:name= ". Smsreceiver ">
<intent-filter>
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
<action android:name= "COM.DONGZI.CUSTOMMSG"/>
</intent-filter>
</receiver>they'll intercept any action-compliant broadcasts.then we'll send the broadcast in the code.
Send custom Broadcasts
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.