Broadcastreceiver Broadcast Receiver Basics

Source: Internet
Author: User

Broadcastreceiver Broadcast Receivers

Broadcast receivers, mainly used for the application to register some designated event broadcast receivers, the system when these events are triggered, will notify the registered broadcast receiver, and then broadcast receiver in the OnReceive function docking received notification processing;
Two ways to register a broadcast receiver:
Static registration:
<receiver android:name= ". Smsbroadcastreceiver ">
<intent-filter>
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver >

Dynamic registration:
Intentfilter intentfilter=new intentfilter ("Android.provider.Telephony.SMS_RECEIVED");
Registerreceiver (Msmsbroadcastreceiver, Intentfilter);

Unlock Registration
Unregisterreceiver (receiver);

The life cycle of Broadcastreceiver
Object call end, OnReceive, Broadcastreceiver object created

Broadcast OnReceive generally call the maximum time of 10 seconds (and possibly shorter), if more than 10 seconds, it is very likely to be the system to end; so some time-consuming tasks, we need to send service to deal with;
(originally wrote a simple text message automatically reply the app, in OnReceive inside created a thread, and will handle the reply task to this thread processing, later found that often did not reply the text message, until later knew Broadcastreceiver has this limit)

Other broadcast reception
Additional registration methods for broadcast receivers:

private void Registerscreenactionreceiver () {
Final Intentfilter filter = new Intentfilter ();
Filter.addaction (Intent.action_screen_off);
Filter.addaction (intent.action_screen_on);
Registerreceiver (receiver, filter);
}

Private Broadcastreceiver receiver = new Broadcastreceiver () {
@Override
public void OnReceive (final context context, final Intent Intent) {
Do your action here

}
}


Through a intentfilter, the corresponding action is added, and then registered, which is registered screen lock and unlock the broadcast receiver registration; receiver is an anonymous class implementation of broadcast;

The switch event action, which can be added to the manifest.xml, increases the broadcast receiver configuration of the action
<action android:name= "Android.intent.action.BOOT_COMPLETED"/> Boot complete
<action android:name= "Android.intent.action.ACTION_SHUTDOWN"/> Shutdown
Android.intent.action.BATTERY_CHANGED Power Change (power change can get battery information in Registerreceiver, returned intent)
<action android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/> Network status changes


Require permissions
<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.BATTERY_STATS"/>

There are other radio receivers to use, need to be in the later study slowly sorted out;

Broadcastreceiver Broadcast Receiver Basics

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.