Android listens to WiFi broadcasts in two ways

Source: Internet
Author: User
Tags valid

1.XML Declaration

<receiver 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>
</receiver>

2. Register in 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. To achieve

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 {@Override public void onreceive (context con Text, Intent Intent) {if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals (Intent.getaction ())) {//This listening WIFI is turned on and off, with W
            IFI connection Independent 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 listening WiFi connection state is connected to a valid no line, when the state of the top broadcast is WifimanageR.wifi_state_disabling, and wifi_state_disabled, will not receive this broadcast at all. Broadcast at the top of the broadcast is wifimanager.wifi_state_enabled state and will also receive this broadcast, of course, just open WIFI must not be connected to a valid wireless if (Wifimanager.network_state_cha Nged_action.equals (Intent.getaction ())) {parcelable Parcelableextra = intent. Getparcelab
            Leextra (Wifimanager.extra_network_info);
                if (null!= parcelableextra) {networkinfo networkinfo = (networkinfo) Parcelableextra;
                state state = Networkinfo.getstate (); Boolean isconnected = state = = state.connected;//Of course, this side can be more accurate to determine the status log.e ("H3C", "isconnected" + isconnected
                ); if (isconnected) {} else {}}}//This listener network connection settings, including WiFi and mobile data
        Open and close. The best thing to use is this listening. WiFi if turned on, turned off, and connections available on the connection will be monitored. See Log//The biggest disadvantage of this broadcast is slower than the two broadcasts on the top, if only to monitor WiFi, I think it is better to use the top two fit if (ConnectivityManager.CONNECTIVITY_ACTION.eq Uals (intent.getactiOn ()) {&lt;p&gt; Connectivitymanager manager = (connectivitymanager) context. Getsystemservice (context.connectivity_
 	SERVICE);
 	Networkinfo GPRS = Manager. Getnetworkinfo (Connectivitymanager.type_mobile); Networkinfo WiFi = Manager. Getnetworkinfo (Connectivitymanager.type_wifi);&lt;/p&gt;&lt;p&gt; log.i (TAG, "Network state Change:" + W
                    ifi.isconnected () + "3g:" + gprs.isconnected ());&lt;/p&gt;&lt;p&gt; 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 ()) {}}} }}}&lt;/p&gt;
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.