Android Broadcast recipient

Source: Internet
Author: User

In fact, in what is the first sentence of the broadcast, it has been explained what is the use of broadcasting. By the way, the general point is to transfer the data. The specific point is that:
1. Data transmission and sharing between different programs is achieved, because the broadcast is acceptable to the recipient as long as it is the same as the action that sent the broadcast.
The typical application is Android's own SMS, phone, and so on, as long as we achieve their action broadcast, then we can receive their data,
To make some processing. Like intercept system messages, intercept harassment calls, etc.
2. The role of a notification, such as in the service to notify the main program, update the main program UI, etc.

Two ways to register a broadcast:

Android offers two types of registered broadcast recipients,
are dynamically registered in the program and specified in the XML, respectively. The difference between them is that the scope of action is different,
The program is dynamically registered: The recipient is only valid during the program's operation,
And in the XML register: The recipient doesn't matter if your program has a startup, it works.

The broadcast sender is divided into ordinary broadcasting and orderly broadcasting;
Synchronous broadcast: When the sender sends out, it reaches more than one broadcast receiver at the same time.
A recipient cannot receive a broadcast after it has been processed and passed to the next recipient.
and unable to stop the broadcast from continuing to spread; Context.sendbroadcast (intent);

Ordered broadcast: The broadcast receiver needs to set priority ahead, priority first received broadcast, priority value is -1000~1000

Receive broadcast:

Private final String action_name = "Send broadcast";

Method of initialization
Public voidRegisterboradcastreceiver () {intentfilter myintentfilter=NewIntentfilter (); Myintentfilter.addaction (Action_name); //Register a broadcastregisterreceiver (mbroadcastreceiver, myintentfilter); } PrivateBroadcastreceiverMbroadcastreceiver=NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action.equals (action_name)) {Toast.maketext (Test. This, "processing the broadcast corresponding to the action name", 200); } } };


Send broadcast:

  

 Public void Sendboradcastreceiver () {         new  Intent (action_name);         Mintent.putextra ("name", "send broadcast, equivalent to transmit data here");                                // Send broadcast            sendbroadcast (mintent);  }  

Method of stopping:

unregisterReceiver(mBroadcastReceiver);

Android Broadcast recipient

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.