Android Network connection related broadcast

Source: Internet
Author: User


Create a new class to inherit Broadcastreceiver, to receive broadcasts, and to handle them accordingly.

Class Netchangereceiver extends Broadcastreceiver {@Override public void onreceive (context context, Intent Intent)        {String action = intent.getaction ();        if (Action.equals (connectivitymanager.connectivity_action)) {System.out.println ("Network status change");    }else if (action.equals (ethernetmanager.ethernet_state_changed_action)) {System.out.println ("Network cable status Change");} }}

Registering a broadcast receive filter in the Androidmenifest.xml <application></application> tag

<!--network change broadcast--><receiver android:name= "Com.coship.entropic.factorytest.receiver.NetChangeReceiver" > <intent-filter> <action android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> ;</receiver><!--Network cable status change broadcast--><receiver android:name= " Com.coship.entropic.factorytest.receiver.NetChangeReceiver "> <intent-filter> <action android:name=" a Ndroid.net.ethernet.ETHERNET_STATE_CHANGED "/> </intent-filter></receiver>


Receiving a broadcast requires only the two steps above, and of course, configuring permissions

<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/><uses-permission android:name= " Android.permission.CHANGE_WIFI_STATE "/><uses-permission android:name=" android.permission.ACCESS_NETWORK_ State "/>

After completion, the above two System.out.println (), the output content is not visible in the console, because the network status changes will cause the Android device and the computer connection disconnect, so the console will not have output. If you want to test whether you can receive broadcasts, you can detect them in other ways, such as

Handler.sendemptymessage (0);

Notification interface Pop-up information, or print directly to the screen display




The above content online a lot of information, just one need to point out is

<action android:name= "Android.net.ethernet.ETHERNET_STATE_CHANGED"/>

Since this use is relatively small, so the information on the Internet is very small. Here is a list of the action related to wired connection

Connectivitymanager.connectivity_action//Network status change//corresponding action.equals (connectivitymanager.connectivity_action) EthernetManager.ETHERNET_START_INTERFACE_ACTIONEthernetManager.ETHERNET_STATE_CHANGED_ ACTIONEthernetManager.ETHERNET_STOP_INTERFACE_ACTIONEthernetManager.NETWORK_CONNECTED_ ACTIONEthernetManager.NETWORK_DISCONNECTED_ACTIONEthernetManager.NETWORK_STATE_CHANGED_ACTION//Network cable status Change// Corresponds to Action.equals (ethernetmanager.ethernet_state_changed_action)//hot-swap event with corresponding network cable


Android Network connection related broadcast

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.