One of the four Android components: BroadcastReceiver

Source: Internet
Author: User

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>



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.