java代码:
package com.hes.tools.net.interfaced;
public abstract interface INetWork {
public abstract boolean openMobleNetwork();
public abstract boolean closeMobleNetwork();
public abstract boolean getNetworkConnected();
public abstract boolean getMobleNetworkConnected();
public abstract boolean getWifiNetworkConnected();
public abstract boolean getWifiIsOpen();
public abstract void openWifi();
public abstract void closeWifi();
}
package com.hes.tools.net;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.util.Log;
import com.hes.tools.net.interfaced.INetWork;
/**
* @author HES
*/
public class NetWork implements INetWork {
private static NetWork network;
private ConnectivityManager mCM;
private WifiManager mWifiManager;
private NetWork(Context context) {
mCM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
public static NetWork getNetWork(Context context){
if(network == null){
network = new NetWork(context);
}
return network;
}
public boolean openMobleNetwork(){
return mobleNetwork(true);
}
public boolean closeMobleNetwork(){
return mobleNetwork(false);
}
public ConnectivityManager getConnectivityManager(){
if(mCM != null){
return mCM;
}
return null;
}
public WifiManager getWifiManager(){
if(mWifiManager != null){
return mWifiManager;
}
return null;
}
public boolean getNetworkConnected(){
NetworkInfo netinfo = mCM.getActiveNetworkInfo();
if(netinfo != null && netinfo.isConnected() && netinfo.isAvailable() && netinfo.getState() == State.CONNECTED){
Log.d("TOOL", "network connected");
return true;
}
return false;
}
public boolean getMobleNetworkConnected(){
if(getNetworkConnected()){
NetworkInfo info = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(info.getState() == State.CONNECTED){
Log.d("TOOL", "Moble network connected");
return true;
}
}
return false;
}
public boolean getWifiNetworkConnected(){
if(getNetworkConnected()){
NetworkInfo info = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(info.getState() == State.CONNECTED){
Log.d("TOOL", "Wifi network connected");
return true;
}
}
return false;
}
public boolean getWifiIsOpen(){
int state = mWifiManager.getWifiState();
if(state == WifiManager.WIFI_STATE_ENABLED){
return true;
}
return false;
}
public void openWifi(){
mWifiManager.setWifiEnabled(true);
}
public void closeWifi(){
mWifiManager.setWifiEnabled(false);
}
private boolean mobleNetwork(boolean status){
boolean net = false;
try {
Class<?> conmanClass = Class.forName(mCM.getClass().getName());
Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
Object iConnectivityManager = iConnectivityManagerField.get(mCM);
Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, status);
net = true;
} catch (Exception e) {
net = false;
}
return net;
}
/**
* 打开数据网络
* @param context
* @param en
*/
@Deprecated
public static void open(Context context, boolean en) {
try {
ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conmanClass = null;
Field iConnectivityManagerField = null;
Object iConnectivityManager = null;
conmanClass = Class.forName(conman.getClass().getName());
iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
iConnectivityManager = iConnectivityManagerField.get(conman);
Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, en);
} catch (Exception e) {
}
}
}