Android's Broadcastreceiver

Source: Internet
Author: User

Broadcastreceiver is also one of the four components of Android.

First of all, I have a little understanding of Broadcastreceiver: Broadcastreceiver is mainly used to collect messages, including the system is not enough battery tips or to receive text messages when the thing to do.

More official understanding: In Android, broadcast is a widely used mechanism for transferring information between applications. Broadcastreceiver is a class of components that are filtered to receive and respond to the sent broadcast.

Write some examples.

A small example of the broadcast of a receiving system.

An example is a hint about a broken network.

1. Write a class yourself, inherit Broadcastreceiver

Package Zf.receiver;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.widget.toast;public class Myreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (Context context , Intent Intent) {//TODO auto-generated method Stubif (!isnetworkavailable (context)) {Toast.maketext (context, "Pro, off the net!) ", Toast.length_short). Show ();}} Determines whether the public static Boolean isnetworkavailable (context context) is disconnected, {connectivitymanager mgr = (connectivitymana          GER) Context.getsystemservice (Context.connectivity_service);          Networkinfo[] info = Mgr.getallnetworkinfo (); if (info = null) {for (int i = 0; i < info.length; i++) {if (info[i].getstate () = = = Net                  WorkInfo.State.CONNECTED) {return true;      }}} return false; } }

2. Register in the Androidmanifext.xml file

<receiver    android:name= "Zf.receiver.MyReceiver" >    <intent-filter>        <action android: Name= "Android.net.conn.CONNECTIVITY_CHANGE"/>        <category android:name= "Android.intent.category.DEFAULT "/>    </intent-filter></receiver>

Allow networked operations at the same time

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>

This allows the network to be disconnected when it is run.

Register yourself with a broadcast and then broadcastreceiver receive a small example.

1. In the activity

public void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.main);                BTN = (Button) Findviewbyid (r.id.button01);        Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent intent= new Intent (); Intent.setaction (my_action);//Add additional information Intent.putextra for intent ("MSG", "Day, I Grass");//Issue Broadcast sendbroadcast (intent);});    }

2. Registering in the Androidmanifext.xml file

<receiver              android:name= "myreceive" >              <intent-filter>                 <action android:name= " Com.chaowen.action.MY_ACTION "/>              </intent-filter>           </receiver>

3. Create a class to inherit Broadcastreceiver and implement its onreceiver

Package Com.chaowen;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.widget.toast;/** * Receive broadcast * @author Chaowen * */public class Myreceive extends BROADCA streceiver {@Overridepublic void OnReceive (context context, Intent Intent) {     //Get information from Intent string msg = Intent.getstringextra ("msg");        Use Toast to display Toast.maketext (context, MSG, Toast.length_long). Show ();}}

That's all.

There should be more places to add.

Android's Broadcastreceiver

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.