Nonsense don't say that directly on the code
<pre name= "code" class= "Java" >
public class Networkutil {
/** network status is not initialized. Please call the init (Context) method */
public static final int network_unknown =-1;
/** network status is not available (WiFi and GPRS are not started and connected) */
public static final int network_unavailable = 0;
/** WiFi is connected */
public static final int network_wifi_connected = 1;
/** WiFi is not started, GPRS is connected */
public static final int network_gprs_connected = 2;
/** WiFi is started but not connected to any hotspot */
public static final int network_wifi_enabled = 3;
/** switch the WiFi access point */
public static final int network_action_wifi_point_changed = 4;
/** when WiFi is turned on, switch to the status of connected WiFi */
public static final int network_action_wifi_enabled_to_connected = 5;
/** WiFi lost connection, but still in boot state */
public static final int network_action_wifi_connected_to_enabled = 6;
public static final int network_action_unknown =-1;
public static final int network_action_turn_to_available = 0;
public static final int network_action_turn_to_unavailable = 1;
public static final int network_action_gprs_to_wifi = 2;
public static final int network_action_wifi_to_gprs = 3;
private static int networkstate = Network_unknown;
Used in the Notifynetworkstatechanged method. Save the name of the current WiFi connection to check if the WiFi access point has been switched
private static String currentssid = null;
/**
* Gets the current network connection status.
*
* @return Networkutil.network_unavailable,
* networkutil.network_wifi_connected,
* networkutil.network_gprs_connected, Networkutil.
* network_wifi_enabled
*/
public static int Getnetworkstate () {
if (networkstate = = Network_unknown) {
Init ();
}
return networkstate;
}
public static void Init () {
Context context = Mainapplication.getcontext ();
Connectivitymanager Connectivitymanager = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Wifimanager Wifimanager = (wifimanager) context
. Getsystemservice (Context.wifi_service);
state wifistate = null;
if (Connectivitymanager.getnetworkinfo (
CONNECTIVITYMANAGER.TYPE_WIFI) = null) {
Wifistate = Connectivitymanager.getnetworkinfo (
Connectivitymanager.type_wifi). GetState ();
}
state gprsstate = null;
if (Connectivitymanager.getnetworkinfo (
Connectivitymanager.type_mobile) = null) {
Gprsstate = Connectivitymanager.getnetworkinfo (
Connectivitymanager.type_mobile). GetState ();
}
if (wifimanager.getwifistate () = = wifimanager.wifi_state_enabled) {
if (wifistate = = state.connected) {
Networkstate = network_wifi_connected;
} else {
Networkstate = network_wifi_enabled;
// }
} else {
if (gprsstate = = state.connected) {
Networkstate = network_gprs_connected;
} else {
Networkstate = network_unavailable;
// }
// }
if (wifimanager.getwifistate () = = wifimanager.wifi_state_enabled) {
if (wifistate = = state.connected) {
Networkstate = network_wifi_connected;
} else {
if (gprsstate = = state.connected) {
Networkstate = network_gprs_connected;
} else {
Networkstate = network_wifi_enabled;
}
}
} else {
if (gprsstate = = state.connected) {
Networkstate = network_gprs_connected;
} else {
Networkstate = network_unavailable;
}
}
}
/** sends a broadcast that the network status has changed. */
public static void notifynetworkstatechanged (context context,
int statebefore, int stateafter) {
LOG.E ("notifynetworkstatechanged", "Statebefore =" + Statebefore
+ ", Stateafter =" + Stateafter);
int code = Network_action_unknown;
if (Statebefore = = Network_unavailable && stateafter! = Network_unknown
&& stateafter! = network_unavailable) {//network status not available for conversion to usable
Code = network_action_turn_to_available;
} else if (statebefore! = Network_unknown
&& Statebefore! = network_unavailable
&& Stateafter = = network_unavailable) {//network status available for conversion to unavailable
Code = network_action_turn_to_unavailable;
} else if (Statebefore = = network_gprs_connected
&& (Stateafter = = Network_wifi_connected | | stateafter = network_wifi_enabled)) {//GPRS converted to WIFI
Code = Network_action_gprs_to_wifi;
} else if ((Statebefore = = Network_wifi_connected | | statebefore = = network_wifi_enabled)
&& Stateafter = = network_gprs_connected) {//WiFi conversion to GPRS
Code = NETWORK_ACTION_WIFI_TO_GPRS;
} else if (Statebefore = = network_wifi_connected
&& Stateafter = = network_wifi_connected) {//do not turn off WiFi, just switch WiFi access point
Wifimanager Wifimanager = (wifimanager) context
. Getsystemservice (Context.wifi_service);
Get the name of the WiFi access point
String SSID = Wifimanager.getconnectioninfo (). Getssid ();
if (currentssid! = null &&!currentssid.equals (SSID)) {
Code = network_action_wifi_point_changed;
}
} else if (Statebefore = = network_wifi_enabled
&& Stateafter = = network_wifi_connected) {//WiFi access point connected
Code = network_action_wifi_enabled_to_connected;
} else if (Statebefore = = network_wifi_connected
&& Stateafter = = network_wifi_enabled) {//WiFi and access point lost connection
Code = network_action_wifi_connected_to_enabled;
}
If it is a WiFi connection, save the current SSID to check if the WiFi connection has been replaced.
if (Stateafter = = network_wifi_connected) {
Wifimanager Wifimanager = (wifimanager) context
. Getsystemservice (Context.wifi_service);
String SSID = Wifimanager.getconnectioninfo (). Getssid ();
Currentssid = SSID; Save the current SSID
}
if (Code! = Network_action_unknown) {
Intent Intent = new Intent ();
Intent.setaction ("Com.suntek.mway.mobilepartner.NETWORK_STATE_CHANGED");
Intent.putextra ("code", code);
Context.sendbroadcast (Intent);
}
}
/** returns whether a network is currently available */
public static Boolean isnetworkavailable () {
Connectivitymanager connmgr = (Connectivitymanager) mainapplication.getcontext ()
. Getsystemservice (Context.connectivity_service);
Networkinfo networkinfo = Connmgr.getactivenetworkinfo ();
Return (Networkinfo! = null && networkinfo.isconnected ());
}
/** returns whether the WiFi signal is weak */
public static Boolean Iswifirssiweak (context context) {
int Rssi = Getwifirssi (context);
0 ~-50 signal Strongest,-50 ~-70 Signal general, 70 ~-100 Signal Worst
if (Rssi <-70) {//Signal worst
return true;
} else if (Rssi <-60) {//Signal general
return false;
} else {//the strongest signal
return false;
}
}
/** returns whether the WiFi signal is weak */
public static Boolean iswifirssiweak (int rssi) {
0 ~-50 signal Strongest,-50 ~-70 Signal general, 70 ~-100 Signal Worst
if (Rssi <-70) {//Signal worst
return true;
} else if (Rssi <-60) {//Signal general
return false;
} else {//the strongest signal
return false;
}
}
/** return WiFi signal strength level 0-2 */
public static int Getwifirssilevel (int rssi) {
0 ~-50 signal Strongest,-50 ~-70 Signal general, 70 ~-100 Signal Worst
if (Rssi <-70) {//Signal worst
return 0;
} else if (Rssi <-50) {//Signal general
return 1;
} else {//the strongest signal
return 2;
}
}
/** Get WiFi Signal strength */
public static int Getwifirssi (context context) {
Wifimanager Wifimanager = (wifimanager) context
. Getsystemservice (Context.wifi_service);
Wifiinfo wifiinfo = Wifimanager.getconnectioninfo ();
return Wifiinfo.getrssi ();
}
/** Determine whether the network is currently mobile */
public static Boolean Isusechinamobile (context context) {
if (! Servicemanager.isphoneinservice ()) {
return false;
}
Telephonymanager Telmanager = (telephonymanager) context
. Getsystemservice (Context.telephony_service);
String operator = Telmanager.getsimoperator ();
if (operator! = null) {
Loghelper.trace ("operator=" + operator);
if (Operator.equals ("46000") | | | operator.equals ("46002")) {
Mobile
return true;
} else if (Operator.equals ("46001")) {
China Unicom
return false;
} else if (Operator.equals ("46003")) {
Telecom
return false;
}
}
return false;
}
/** determine whether the current WiFi network */
public static Boolean Isconnectwifi () {
Context context = Mainapplication.getcontext ();
Boolean iswificonnect = false;
Connectivitymanager cm = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
networkinfo[] Networkinfos = Cm.getallnetworkinfo ();
for (int i = 0; i < networkinfos.length; i++) {
if (networkinfos[i].getstate () = = NetworkInfo.State.CONNECTED) {
if (networkinfos[i].gettype () = = Connectivitymanager.type_mobile) {
Iswificonnect = false;
}
if (networkinfos[i].gettype () = = Connectivitymanager.type_wifi) {
Iswificonnect = true;
}
}
}
return iswificonnect;
}
}
Android Utility Network Chapter (i)