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