I. unordered broadcast application scenarios: Automatic startup, battery power change, time change, and other broadcasts
Method 1 of conventional Broadcast Reception (static registration ):
| Add the <author ER> label to the <application> label of the configuration file. |
| Example of a call Broadcast |
<Cycler android: name = ". ipcallerpolicer"> <Intent_filter> <Action android: name = "android. intent. action. NEW_OUTGOING_CALL"> </Intent_filter> </Cycler> |
| |
| Write a class to inherit BroadcastReceiver |
| OnReceive Implementation Method |
| String number = getResultData (); // get data |
| If (number. startsWith ("0516") // all calls to Xuzhou use IP dialing. |
| { |
| Number = "17951" + number; |
SerResultData (number );
|
| } |
Note: To ensure the smoothness of user interaction, some time-consuming operations should be put into the thread, such as the class name: SMSBroadcastReceiver
Conventional method of receiving broadcast 2 (Dynamic Registration): Generally, BroadcastReceiver is registered in onResume () when the Activity can interact.
| IntentFilter intentFilter = new IntentFilter ("android. provider. Telephony. SMS_RECEIVED "); |
| RegisterReceiver (mBatteryInfoReceiver, intentFilter ); |
| |
| UnregissterReceiver (assumer); // unregister |
Note: The life cycle is only about 10 seconds. If you do more than 10 seconds in onReceive (), the ANR (Application NO Response) program will report NO Response error, if you need to complete a time-consuming task, you should send Intent to the Service, which is done by the Service.
Custom unordered broadcast:
| Intent intent = new Intent (); |
| Intent. setAction ("com. mmovve. jiuming"); // custom broadcast action |
| Context. sendBroadcast (intent ); |
| |
| Define broadcast Receiver |
| Add the <author ER> label to the <application> label of the configuration file. |
<Cycler android: name = ". WBHReceiver"> <Intent-filter> <Action android: name = "com. mmovve. jiuming"/> </Intent-filter> </Cycler> |
| Write a class to inherit BroadcastReceiver |
| OnReceive Implementation Method |
2. Ordered broadcast Context. sendOrderedBroadcast (intent, receiverPermission); the second parameter is broadcast level. The level range is-1000 ~ 1000, the higher the level, the higher the broadcast priority. The broadcast receiver level is set by the priority in the <intent-filter> label. The higher the value, the higher the acceptance priority. For advanced or same-level first-received broadcasts, you can use the abortBroadcast () method to stage broadcasts so that other recipients cannot receive the broadcasts.
<Cycler android: name = ". Lv3">
<Intent-filter android: priority = "500">
<Action android: name = "com. mmovve. jiuming"/>
</Intent-filter>
</Cycler>