Two Methods for Android to listen to wi-fi broadcasts

Source: Internet
Author: User

1. Declaration in XML

<Cycler android: name = ". NetworkConnectChangedReceiver">
<Intent-filter>
<Action android: name = "android.net. conn. CONNECTIVITY_CHANGE"/>
<Action android: name = "android.net. wifi. WIFI_STATE_CHANGED"/>
<Action android: name = "android.net. wifi. STATE_CHANGE"/>
</Intent-filter>
</Cycler>

2. Register in the code
IntentFilter filter = new IntentFilter ();
Filter. addAction (WifiManager. NETWORK_STATE_CHANGED_ACTION );
Filter. addAction (WifiManager. WIFI_STATE_CHANGED_ACTION );
Filter. addAction (ConnectivityManager. CONNECTIVITY_ACTION );
RegisterReceiver (new NetworkConnectChangedReceiver (), filter );

3. Implementation

Package com. example. testss; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android.net. connectivityManager; import android.net. networkInfo; import android.net. networkInfo. state; import android.net. wifi. wifiManager; import android. OS. parcelable; import android. util. log; public class NetworkConnectChangedReceiver extends BroadcastReceiver {@ Over Ride public void onReceive (Context context, Intent intent) {if (WifiManager. WIFI_STATE_CHANGED_ACTION.equals (intent. getAction () {// enable and disable the listener wifi. It has nothing to do with the connection of the wifi. int wifiState = intent. getIntExtra (WifiManager. EXTRA_WIFI_STATE, 0); Log. e ("H3c", "wifiState" + wifiState); switch (wifiState) {case WifiManager. WIFI_STATE_DISABLED: break; case WifiManager. WIFI_STATE_DISABLING: break; //} // This listens to the Wi-Fi The connection status indicates whether a valid wireless route is connected. When the broadcast status is WifiManager. WIFI_STATE_DISABLING and WIFI_STATE_DISABLED, the broadcast is not received. // WifiManager is used to broadcast data to IOT platform. WIFI_STATE_ENABLED status will also receive this broadcast, of course, just opened wifi is certainly not connected to a valid wireless if (WifiManager. NETWORK_STATE_CHANGED_ACTION.equals (intent. getAction () {Parcelable parcelableExtra = intent. getParcelableExtra (WifiManager. EXTRA_NETWORK_INFO); if (null! = ParcelableExtra) {NetworkInfo networkInfo = (NetworkInfo) parcelableExtra; State state = networkInfo. getState (); boolean isConnected = state = State. CONNECTED; // Of course, you can determine the status Log more accurately. e ("H3c", "isConnected" + isConnected); if (isConnected) {} else {}}// set the network connection for this listener, including enabling and disabling wifi and mobile data .. // It is best to use this listener. If wifi is enabled, disabled, and connections available on the connection are listened. See log // The biggest drawback of this broadcast is that it is slower than the response of the above two broadcasts. if you only want to listen to wifi, I think it is more appropriate to use the above two combinations if (ConnectivityManager. CONNECTIVITY_ACTION.equals (intent. getAction () {<p> ConnectivityManager manager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo gprs = manager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE); NetworkInfo wifi = manager. getNetworkInfo (ConnectivityManager. TYPE_WIFI); </p> <P> Log. I (TAG, "network status changed:" + wifi. isConnected () + "3g:" + gprs. isConnected (); </p> <p> NetworkInfo info = intent. getParcelableExtra (ConnectivityManager. EXTRA_NETWORK_INFO); if (info! = Null) {Log. e ("H3c", "info. getTypeName () "+ info. getTypeName (); Log. e ("H3c", "getSubtypeName ()" + info. getSubtypeName (); Log. e ("H3c", "getState ()" + info. getState (); Log. e ("H3c", "getDetailedState ()" + info. getDetailedState (). name (); Log. e ("H3c", "getDetailedState ()" + info. getExtraInfo (); Log. e ("H3c", "getType ()" + info. getType (); if (NetworkInfo. state. CONNECTED = info. getState () {} else if (info. getType () = 1) {if (NetworkInfo. state. DISCONNECTING = info. getState () {}}}}</p>

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.