Basic: 4. Familiar with receiving and using broadcastreceiver

Source: Internet
Author: User

1. Broadcast

  Since we want to talk about broadcast receivers, we must first talk about broadcasting. After all, the two complement each other. In the Android system, there are a variety of broadcasts, such as the Usage Status of battery, telephone answering, and SMS receiving. Application developers can also send various broadcasts in programs. So what is broadcast ?! --- Broadcast isWidely used mechanism for transmitting information between applications. You can send a broadcast in either of the following ways:

(1) Context. sendbroadcast --- broadcast unordered events. Theoretically, all receivers receive broadcasts at the same time.

(2) context. sendorderedbroadcast --- broadcast ordered time, which is received by the receiver in the specified order. In this way, the information received by each receiver may be different, lower-priority Receivers may even fail to receive broadcasts (if the previous receiver terminates the broadcast ).

The Broadcast Mechanism of Android is well designed. Many tasks that require developers to manually monitor and operate are now only required to listen to broadcasts and perform relevant operations, at the same time, Android provides a development tool for listening to various broadcasts-broadcastreceiver.

2. Broadcast Receiver

  The broadcast receiver is an intent used to receive broadcasts asynchronously. broadcastreceiver does not have a visual user interface. However, after receiving a broadcast, you can enable an activity or icationicationmanager to notify the user. To enable broadcastreceiver to run as intended, You need to perform the following two steps:

(1) Create your own broadcastreceiver class, which inherits from Android. content. broadcastreceiver and implements its onreceive method.

public class MyReceiver extends BroadcastReceiver {            private static final String TAG = "MyReceiver";            @Override      public void onReceive(Context context, Intent intent) {          String msg = intent.getStringExtra("msg");          Log.i(TAG, msg);      }    }  

Users can perform their own operations in onreceive, such as opening activity and notificationmanager.

(2) register a specified broadcast address

A) static registration: static registration refers to the configuration in androidmanifest. xml:

<receiver android:name=".MyReceiver">              <intent-filter>                  <action android:name="android.intent.action.MY_BROADCAST"/>                  <category android:name="android.intent.category.DEFAULT" />              </intent-filter>  </receiver>  

Here, Action Android: Name... specifies that the broadcast receiver can receive the broadcast of the specified address. In other words, the broadcast receiver registers the broadcast of the address.

B) Dynamic Registration: dynamically specify the broadcast address in the Code and register it:

MyReceiver receiver = new MyReceiver();            IntentFilter filter = new IntentFilter();  filter.addAction("android.intent.action.MY_BROADCAST");          registerReceiver(receiver, filter);  

For a dynamically registered receiver, You must contact the registration operation before the program is destroyed. Otherwise, the system will have an exception and the process of unregistering is simple:

protected void onDestroy() {      super.onDestroy();      unregisterReceiver(receiver);  }  

The biggest difference between static registration and dynamic registration is that the broadcast receiver of static registration is long resident, that is, if broadcast information is sent after the program is closed, the broadcast receiver is also automatically called and run by the system.

There is also a deeper layer of knowledge about broadcastreceiver and broadcast, which will be further supplemented when more contacts and deeper understandings are available in the future.

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.