BroadcastReceive broadcast receiver :,

Source: Internet
Author: User

BroadcastReceive broadcast receiver :,

BroadcastReceive broadcast receiver: public class Test extends Activity {private final String ACTION_NAME = "Send broadcast"; private Button mBtnMsgEvent = null; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // register the broadcast registerBoradcastReceiver (); LinearLayout mLinearLayout = new LinearLayout (this); mBtnMsgEvent = new Button (this); mBtnMsgEvent. setText ("Send broadcast"); mLinearLayout. addView (mB TnMsgEvent); setContentView (mLinearLayout); mBtnMsgEvent. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent mIntent = new Intent (ACTION_NAME); mIntent. putExtra ("yaner", "Send broadcast, equivalent to transmitting data here"); // send broadcast sendBroadcast (mIntent) ;}});} private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent inten T) {String action = intent. getAction (); if (action. equals (ACTION_NAME) {Toast. makeText (Test. this, "broadcast corresponding to processing action name", 200) ;}}; public void registerBoradcastReceiver () {IntentFilter myIntentFilter = new IntentFilter (); myIntentFilter. addAction (ACTION_NAME); // register the broadcast registerReceiver (mBroadcastReceiver, myIntentFilter) ;}// disable broadcast protected void onDestroy () {// TODO Auto-generated method stub s Uper. onDestroy (); unregisterReceiver (assumer);} Your application can use it to filter external events that are only interested in (for example, when a call is made, or when the data network is available. The broadcast receiver has no user interface. However, they can start an activity or serice to respond to the information they receive, or use icationicationmanager to notify users. Notifications can be used in many ways to attract users' attention-flashing back lights, vibrations, and playing sounds. In general, a persistent icon is placed on the status bar. You can open it and get the message. Broadcast type: normal broadcast, through Context. ordered broadcast sent by sendBroadcast (Intent myIntent), through Context. sent by sendOrderedBroadcast (intent, receiverPermission). The 2nd parameters of this method determine the broadcast level. The value range is-1000 to 1000. The higher the value, the higher the sending priority; the level at which the broadcast receiver receives the broadcast (the highest priority can be set through the priority in intentfilter to 2147483647). The same level of receipt is random, when a broadcast is received at a lower level, if the broadcast is received at a higher level or at the same level first, the broadcast can be truncated using the abortBroadcast () method so that other recipients cannot receive the broadcast, there are other constructor asynchronous broadcasts through Context. sendStickyBroadcast (Intent myIntent), and sendStickyOrd The eredBroadcast (intent, resultReceiver, scheduler, initialCode, initialData, and initialExtras) method has the following features: <uses-permission android: name = "android. permission. BROADCAST_STICKY "/> permission. After receiving and processing the Intent, the broadcast still exists until you call removeStickyBroadcast (intent) to actively remove it. Note: The intent parameter when sending the broadcast and Contex. startActivity () starts up with a different Intent. The former can be called by multiple broadcast receivers that subscribe to it, and the latter can only be called by one (Activity or service) to listen to the broadcast Intent step: 1> write a class that inherits BroadCastReceiver and rewrite it. OnReceive () method. The broadcast receiver is active only when it executes this method. When onReceive () is returned, it is inactive. Note: To ensure the smoothness of the user interaction process, some time-consuming operations should be placed in the thread, for example, the class name SMSBroadcastReceiver2> registers the broadcast receiver. There are two methods to register the program dynamically and perform static registration in the AndroidManifest file (which can be understood as registering in the system): static registration, registered broadcast. The following priority indicates that the broadcast receiving level "2147483647" is the highest priority <receiver er android: name = ". SMSBroadcastReceiver "> <intent-filter android: priority =" 2147483647 "> <action android: name =" android. provider. telephony. SMS_RECEIVED "/> </intent-filter> </Cycler>: dynamically register an instance. Generally, onR is used when the Activity can interact. Register BroadcastReceiverIntentFilter intentFilter = new IntentFilter ("android. provider. telephony. SMS_RECEIVED "); registerReceiver (mBatteryInfoReceiver, intentFilter); // unregisterReceiver (receiver) is not registered. Note: 1. the life cycle is only about 10 seconds. If you do things in onReceive () for more than 10 seconds, the ANR (Application No Response) program will report an error message that does not respond, if you need to complete a time-consuming task, you should send Intent to the Service, which is done by the Service. subthreads cannot be used here, because the life cycle of BroadcastReceiver is very short, and the subthread may not end yet. BroadcastReceiver is over. once BroadcastReceiver ends, the process where BroadcastReceiver is located is easily killed first when the system requires memory because it is a blank process (process without any active components ). if its host process is killed, the working sub-thread is also killed. therefore, it is unreliable to use sub-threads. another feature of Dynamic Registration of broadcast receivers is that when the Activity used for registration is disabled, the broadcast will become invalid. Static registration does not need to worry about whether the broadcast receiver is disabled. As long as the device is enabled, the broadcast receiver is also enabled. That is to say, even if the app itself is not started, the broadcast subscribed to by the app will also broadcast Intent to it when triggered, such as startup, battery power change, and time change.

Related Article

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.