Introduction to Android Early-learning-broadcast receiver 01__java

Source: Internet
Author: User

Original address: http://blog.csdn.net/liuhe688/article/details/6955668/

Broadcastreceiver is the "broadcast receiver" meaning, as the name suggests, it is used to receive from the system and applications in the broadcast.

In the Android system, broadcast in all aspects, for example, when the system is completed after a broadcast, received this broadcast will be able to achieve the function of the boot service; When the network state changes, the system will produce a broadcast, receive this broadcast can prompt and save data and so on operation When the battery power changes, the system will produce a broadcast, receive this broadcast can be in low power to tell the user in time to save the progress, and so on.

The broadcast mechanism in Android is designed so well that a lot of things would have to be done by the developers themselves, and now it's just waiting for the broadcast to tell itself, which greatly reduces the development effort and the development cycle. And as an application developer, you need to learn to master the Android system provides a development tool, that is broadcastreceiver. Here we will analyze and drill the Broadcastreceiver to understand and master its various functions and usages.

First, let's demonstrate the creation of a broadcastreceiver and let this broadcastreceiver be able to run according to our needs.

To create your own Broadcastreceiver object, we need to inherit android.content.BroadcastReceiver and implement its OnReceive method. Let's create a radio receiver named Myreceiver:

[Java]  View plain  copy package com.scott.receiver;      import  android.content.broadcastreceiver;   import android.content.context;   import  android.content.intent;   import android.util.log;      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);        }     }  

In the OnReceive method, we can get data from the intent that comes with the broadcast, which is very important, like a radio, that contains a lot of useful information.

After creating our broadcastreceiver, we are not able to get it working, we need to register a specified broadcast address for it. Broadcastreceiver, which does not have a registered broadcast address, is like a radio with a missing button, which, while functional, cannot receive a radio signal. Let's explain how to register a broadcast address for broadcastreceiver.

Static Registration

Static registration is configured in the Androidmanifest.xml file and we are going to register a broadcast address for myreceiver:

[HTML] view plain copy <receiver android:name=. Myreceiver "> <intent-filter> <action android:name=" Android.intent.action.MY_BR Oadcast "/> <category android:name=" Android.intent.category.DEFAULT "/> </inten T-filter> </receiver>

After configuring the above information, as long as it is android.intent.action.MY_BROADCAST the address of the broadcast, Myreceiver can receive. Note that this type of registration is resident, that is, when the application is closed, if there is broadcast information, Myreceiver will also be automatically run by the system call.

Dynamic Registration

Dynamic registration needs to be dynamically specified in the code broadcast address and registration, usually we are in the activity or service register a broadcast, below we take a look at the registered code:

[Java] view plain copy myreceiver receiver = new Myreceiver ();   Intentfilter filter = new Intentfilter ();              Filter.addaction ("Android.intent.action.MY_BROADCAST");   Registerreceiver (receiver, filter); Note that Registerreceiver is a method in the Android.content.ContextWrapper class where both the activity and the service inherit the Contextwrapper, so they can be invoked directly. In practice, we registered a broadcastreceiver in the activity or service, and when the activity or service was destroyed, if it was not released, the system would report an exception, prompting us whether we forgot to cancel the registration. So, remember to perform a cancellation in a particular place:

[Java]

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.