Receives System broadcast messages in Android
A large number of Android system events will send standard broadcasts:
Create a broadcast Action constant:
ACTION_TIME_CHANGED system time changed
ACTION_DATE_CHANGED system date changed
ACTION_TIMEZONE_CHANGED system time zone changed
ACTION_BOOT_COMPLETED system startup completed
ACTION_PACKAGE_ADDED system add package
ACTION_PACKAGE_CHANGED system package change
ACTION_PACKAGE_REMOVED system package Deletion
The package data of ACTION_PACKAGE_RESTARTED is restarted.
The package data of the ACTION_PACKAGE_DATA_CLEARED system is cleared.
ACTION_BATTERY_CHANGED battery power change
ACTION_BATTERY_LOW low battery power
ACTION_POWER_CONNECTED system Power Supply
ACTION_POWER_DISCONNECTED system disconnected from Power Supply
ACTION_SHUTDOWN system disabled
1. Self-starting Service
We often have such applications, such as the message PUSH Service, which must be enabled upon startup.
For example, listening for user calls, listening for user text messages, blocking blacklisted calls, etc.
To enable automatic execution of services as the system starts, you can enable BroadcastReceiver to listen to Intent whose Action is the constant ACTION_BOOT_COMPLETED, and then start a specific Service in BroadcastReceiver.
LaunchReceiver. java
Public class LaunchReceiver extends BroadcastReceiver {/** self-starting Service: * For example, listening for user calls, listening for user text messages, blocking blacklist calls, etc. * to enable automatic execution of the Service as the system starts, broadcastReceiver can listen to Intent whose Action is ACTION_BOOT_COMPLETED constant, and then start * specific Service ***/@ Overridepublic void onReceive (Context context, Intent intent) in BroadcastReceiver) {// TODO Auto-generated method stub intent_to_service = new Intent (context, LaunchService. class); context. startService (intent_to_service); // start a specific Service in broadcast }}
LaunchService. java
Public class LaunchService extends Service {@ Overridepublic IBinder onBind (Intent intent) {// TODO Auto-generated method stubreturn null;} // this method is called back when the Service is created, you can develop any Service, listen to user calls, listen to user text messages, intercept blacklisted phone calls, and so on public void onCreate () {/* // define 1 second to execute a line to output new Timer (). schedule (new TimerTask () {@ Overridepublic void run () {System. out. println ("-----" + new Date () + "-----") ;}}, 0, 1000); */Toast. makeText (this, "system started", Toast. LENGTH_LONG ). show ();}}
2. SMS reminder: when the system receives the text message, it sends a Broadcast, which will trigger Broadcast before the system receives the text message.
When the system receives a text message, the system sends an ordered broadcast. The Intent Action of the broadcast is android. provider. Telephony. SMS_RECEIVED.
Therefore, you only need to develop a corresponding BroadcastReceiver in the program to listen to the system to receive text messages.
SmsReceiver. java
Package com. hust. smsreceiver; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. bundle; import android. telephony. smsMessage; import android. widget. toast;/** SMS reminder: * When the system receives the SMS, the system sends an ordered broadcast to the outside. The Intent Action of the broadcast is android. provider. telephony. SMS_RECEIVED * therefore, you only need to develop a corresponding BroadcastReceiver in the program to listen to the system receiving the SMS **/public class SmsReceiver extends Br OadcastReceiver {// when the system receives the SMS, It is triggered before the system's built-in SMS receiving program. It is equivalent to intercepting the SMS @ Overridepublic void onReceive (Context context, Intent intent) {// TODO Auto-generated method stub // if the message if (intent. getAction (). equals ("android. provider. telephony. SMS_RECEIVED ") {/* will be started before the system's SMS receiving program. If the broadcast is canceled, the SMS broadcasting will not be spread to the system's SMS receiving program, that is, the system itself will not receive a text message * // abortBroadcast (); // cancel the broadcast. This line of code will make the system unable to receive the text message StringBuilder sb = new StringBuilder (); // text message data is bound to the intent Bun Bundle bundle = intent. getExtras (); if (bundle! = Null) {// all received text message objects [] pdus = (Object []) bundle can be obtained through pdus. get ("pdus"); // construct the SMS object array smsmsmessage [] messages = new SmsMessage [pdus. length]; for (int I = 0; I <= pdus. length; I ++) {// converts each pdus into a text message object SmsMessagemessages [I] = SmsMessage according to pdus. createFromPdu (byte []) pdus [I]);} for (SmsMessage message: messages) {sb. append ("SMS Source:"); sb. append (message. getDisplayOriginatingAddress (); // obtain the SMS source address sb. append ("\ n ---- text message content ----- \ n"); sb. append (message. getDisplayMessageBody (); // get the text message content} Toast. makeText (context, sb. toString (), Toast. LENGTH_LONG ). show (); // toast message prompt }}}
3. Power Change
If we read the software, it may be full screen reading. At this time, the user will not be able to see the remaining power, and we will be able to provide them with power information. To achieve this, we need to receive a broadcast of power changes and then get the percentage information, which sounds simple.
When the cell phone power changes, the system will send a broadcast of the constant of Intent action_battery_changed,
When the cell phone power is too low, a broadcast of the constant ACTION_BATTERY_LOW will be sent.
Package com. hust. batteryreceiver; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. bundle; import android. widget. toast; public class BatteryReceiver extends BroadcastReceiver {/* When the cell phone power changes, the system will send a broadcast of the constant Intent action_battery_changed, * When the cell phone power is too low, send the broadcast of ACTION_BATTERY_LOW constant ***/@ Overridepublic void onReceive (Context context, In Tent intent) {// TODO Auto-generated method stub Bundle bundle = intent. getExtras (); int current = bundle. getInt ("level"); // key = level is the current power int total = bundle. getInt ("scale"); // key = scale is the total power. // The current power is less than 15% of the total power. if (current * 1.0/total <0.15) {Toast. makeText (context, "the battery power is too low. Please charge it as soon as possible! ", Toast. LENGTH_LONG). show ();}}}