Android WiFi status three kinds of broadcast

Source: Internet
Author: User

PublicclassNetworkconnectchangedreceiverextendsbroadcastreceiver{
@Override
PublicvoidOnReceive (context context, Intent Intent) {
if(WifiManager.WIFI_STATE_CHANGED_ACTION.equals (Intent.getaction ())) {//This listening WiFi is turned on and off, regardless of the WiFi connection
intWifistate = Intent.getintextra (wifimanager.extra_wifi_state, 0);
Logtag.showtag_e ("WiFi Status", "Wifistate" +wifistate);
Switch(wifistate) {
CaseWifimanager.wifi_state_disabled:
Break;
CaseWifimanager.wifi_state_disabling:
Break;
//  
}
}
//The connection status of the listening WiFi is connected to a valid no-line, when the state of the broadcast above is wifimanager.wifi_state_disabling, and wifi_state_disabled, it will not receive this broadcast.
//Broadcast on the top of the broadcast is the wifimanager.wifi_state_enabled state will also receive this broadcast, of course, just open WiFi must not be 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 ();
Booleanisconnected = state==state.connected;//of course, this way you can determine the state more precisely.
Logtag.showtag_e ( This. GetClass (). Getsimplename (), "isconnected" +isconnected);
if(isconnected) {
}Else{

}
}
}
//This listens for network connection settings, including WiFi and mobile data opening and closing.
//It's better to use this monitor. WiFi is monitored if it is turned on, closed, and the connection is available on the connection. See log
//The biggest drawback of this broadcast is that it is slower than the top two broadcasts, and if you just want to listen to WiFi, I think it's more appropriate to use the top two mates .
if(ConnectivityManager.CONNECTIVITY_ACTION.equals (Intent.getaction ())) {
Networkinfo info = Intent.getparcelableextra (connectivitymanager.extra_network_info);
if(Info! =NULL) {
Logtag.showtag_e ("Connectivity_action", "Info.gettypename ()" +info.gettypename ());
Logtag.showtag_e ("Connectivity_action", "Getsubtypename ()" +info.getsubtypename ());
Logtag.showtag_e ("Connectivity_action", "GetState ()" +info.getstate ());
Logtag.showtag_e ("Connectivity_action",
"Getdetailedstate ()" +info.getdetailedstate (). name ());
Logtag.showtag_e ("Connectivity_action", "Getdetailedstate ()" +info.getextrainfo ());
Logtag.showtag_e ("Connectivity_action", "GetType ()" +info.gettype ());
}
}


if(WifiManager.NETWORK_STATE_CHANGED_ACTION.equals (Intent.getaction ())) {//The connection status of this monitor WiFi
Parcelable Parcelableextra = Intent.getparcelableextra (Wifimanager.extra_network_info);
if(NULL! = Parcelableextra) {
Networkinfo Networkinfo = (networkinfo) Parcelableextra;
state state = Networkinfo.getstate ();
if(state==state.connected) {
showwificconnected (context);
}
/**else if (state==state.disconnected) {
showwifidisconnected (context);
}*///yesterday wrote this method, in the subway when found that if there is invalid WiFi site on the subway, the phone will automatically connect, but the connection failed to receive the broadcast, so it can not be used
}
}
if(ConnectivityManager.CONNECTIVITY_ACTION.equals (Intent.getaction ())) {//This monitor network connection settings, including WiFi and mobile data opening and closing
Networkinfo info = Intent.getparcelableextra (connectivitymanager.extra_network_info);
if(Info! =NULL) {
if(Networkinfo.state.connected==info.getstate ()) {
Intent pushintent =NewIntent ();
Pushintent.setclass (context, Notificationservice.class);
}Elseif(Info.gettype () ==1) {
if(Networkinfo.state.disconnecting==info.getstate ())
showwifidisconnected (context);
}
}
}
}


Android WiFi status three kinds of 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.