Android Radio Listening WiFi

Source: Internet
Author: User

First step: Configure Androidmanifest.xml, add the following code below the application node

<receiverAndroid:name=". Networkconnectchangedreceiver " >     <Intent-filter>         <ActionAndroid:name= "Android.net.conn.CONNECTIVITY_CHANGE" />         <ActionAndroid:name= "Android.net.wifi.WIFI_STATE_CHANGED" />         <ActionAndroid:name= "Android.net.wifi.STATE_CHANGE" />     </Intent-filter></receiver>


Also configure Androidmanifest.xml, add permissions

<android:name= "Android.permission.ACCESS_NETWORK_STATE"/>  <android:name= "Android.permission.ACCESS_COARSE_LOCATION"/> <  android:name= "Android.permission.ACCESS_WIFI_STATE"/>


Step Two: Register for the broadcast

Intentfilter filter =NewIntentfilter ();/*** network_state_changed_action * * WiFi connection is OK **/filter.addaction (wifimanager.network_state_changed_action);/*** wifi_state_changed_action * * There is no WIFI source and changes between the various states **/filter.addaction (wifimanager.wifi_state_changed_action);/*** connectivity_action * wifi if open, close, and connections available on the connection will be monitored * The biggest drawback of this broadcast is slower than the top two radio response, if only to listen to WiFi, I think it is more appropriate to use the top two matching *  */filter.addaction (connectivitymanager.connectivity_action); Registerreceiver (NewNetworkconnectchangedreceiver (NewNetworkconnectchangedactionimp ()), filter);


Step Three:

ImportAndroid.content.Context;Importandroid.content.Intent; Public Interfacenetworkconnectchangedaction {voiddoafterconnected (Context context, Intent Intent); voiddoafterconnecting (Context context, Intent Intent); voiddoafterdisconnected (Context context, Intent Intent); voiddoafterdisconnecting (Context context, Intent Intent); voiddoaftersuspended (Context context, Intent Intent); voidDoafterunknown (Context context, Intent Intent);}

ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.net.ConnectivityManager;ImportAndroid.net.NetworkInfo; Public classNetworkconnectchangedreceiverextendsBroadcastreceiver {Private StaticNetworkconnectchangedaction action =NULL; Static{Action=NewNetworkconnectchangedactionimp (); } @Override Public voidOnReceive (Context context, Intent Intent) {if(ConnectivityManager.CONNECTIVITY_ACTION.equals (Intent.getaction ())) {Connectivitymanager Manager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); /*** GPRS*/            //Networkinfo GPRS = Manager.getnetworkinfo (connectivitymanager.type_mobile);            /*** INFO*/            //Networkinfo info = Intent.getparcelableextra (connectivitymanager.extra_network_info);            /*** WIFI*/Networkinfo WiFi=Manager.getnetworkinfo (Connectivitymanager.type_wifi); if(WiFi! =NULL) {                if(NetworkInfo.State.CONNECTED = =wifi.getstate ())                    {action.doafterconnected (context, intent); } Else if(NetworkInfo.State.CONNECTING = =wifi.getstate ())                {action.doafterconnecting (context, intent); } Else if(NetworkInfo.State.DISCONNECTED = =wifi.getstate ())                {action.doafterdisconnected (context, intent); } Else if(NetworkInfo.State.DISCONNECTING = =wifi.getstate ())                {action.doafterdisconnecting (context, intent); } Else if(NetworkInfo.State.SUSPENDED = =wifi.getstate ())                {action.doaftersuspended (context, intent); } Else if(NetworkInfo.State.UNKNOWN = =wifi.getstate ())                {Action.doafterunknown (context, intent); }            }        }        }}


Fourth Step: Concrete Implementation

ImportAndroid.content.Context;Importandroid.content.Intent; Public classNetworkconnectchangedactionimpImplementsnetworkconnectchangedaction{@Override Public voiddoafterconnected (Context context, Intent Intent) {} @Override Public voiddoafterconnecting (Context context, Intent Intent) {} @Override Public voiddoafterdisconnected (Context context, Intent Intent) {} @Override Public voiddoafterdisconnecting (Context context, Intent Intent) {} @Override Public voiddoaftersuspended (Context context, Intent Intent) {} @Override Public voidDoafterunknown (Context context, Intent Intent) {}}

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.