What is Broadcastreceiver
Broadcastreceiver is one of the four components of Android, which is essentially a global listener for listening to the global broadcast of the system, formally because it is a global listener, so it is very convenient to implement communication between different components in the system. Also convenient for our own app for the system boot, low-power broadcast to make corresponding reflection. We can also send our own broadcasts to enable communication between different components.
The system receives the SMS broadcast and then pops up the toast:
PackageCn.lixyz.broadcastreceiver;Importandroid.app.Activity;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Privateintentfilter Filter; Privatesmsreceiver SMS; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Filter=NewIntentfilter ("Android.provider.Telephony.SMS_RECEIVED"); SMS=NewSmsreceiver (); Registerreceiver (SMS, filter); } @Overrideprotected voidOnDestroy () {//TODO auto-generated Method Stub Super. OnDestroy (); Unregisterreceiver (SMS); } classSmsreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {toast.maketext (context,"There's a message.", Toast.length_short). Show (); LOG.D ("Tttt", "~~~~~~~"); } }}
Mainactivity.java
You also need to add permissions:
<android:name= "Android.permission.RECEIVE_SMS"/>
Operation Result:
A large number of Android system events will be broadcast externally, the following are common broadcast constants:
Action_time_changed system time is changed
action_date_changed system date is changed
action_timezone_changed system time zone is changed
action_boot_completed System Boot Complete
action_package_added System Add Package
Package Change of action_package_changed system
Action_package_removed system's Package is removed
Action_package_restarted system's package is restarted
Package data for action_package_data_cleared system is emptied
action_battery_changed Battery charge Change
Action_battery_low Battery power Low
action_power_connected System Connection Power
action_power_disconnected system disconnected from power supply
Action_shutdown system is turned off
Categories of Broadcast
Broadcast divided into standard broadcast and orderly broadcast
Standard broadcast is a completely asynchronous broadcast, after the broadcast, all broadcast receivers will receive this broadcast almost at the same time, there is no order, the broadcast is efficient, but can not be truncated.
Orderly broadcast is a synchronous broadcast, after the broadcast, only one broadcast receiver will be able to receive this broadcast message at the same time, and when the logic in this broadcast receiver is completed, the broadcast will continue to pass. A broadcast receiver with a high priority can be received first, and the broadcast can be truncated, and subsequent broadcast receivers cannot be received.
Android Note (60) Android Summary: four components--broadcastreceiver