本文共 4135 字,大约阅读时间需要 13 分钟。
轉載自
参考地址:
用户权限:
判断有无网络连接:
ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE); //检查网络连接 NetworkInfo info = mConnectivity.getActiveNetworkInfo(); if (info == null || !mConnectivity.getBackgroundDataSetting()) { return false; }
判断WiFi是否已连接:
/** * make true current connect service is wifi * @param mContext * @return */ private static boolean isWifi(Context mContext) { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } return false; }
判断WiFi和移动流量是否已连接:
public static boolean checkNetworkConnection(Context context) { final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if(wifi.isAvailable()||mobile.isAvailable()) //getState()方法是查询是否连接了数据网络 return true; else return false; }
只判断移动网络连接是否正常:
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); //获取移动网络信息 if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); //getState()方法是查询是否连接了数据网络 } } return false;
一个工具类:
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.provider.Settings; import android.telephony.TelephonyManager; import android.util.Log; public class NetWorkUtils { // Context context; public static boolean isNetWorkAvaliable(Context context){ ConnectivityManager connect = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); if(connect == null){ return false; }else{ NetworkInfo[] info = connect.getAllNetworkInfo(); if(info != null){ for(int i=0;iwifiManager.setWifiEnabled(true); // } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(ipAddress); return ip; } private static String intToIp(int i) { return (i & 0xFF ) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ( i >> 24 & 0xFF) ; } //使用GPRS public static String getLocalIpAddress() { try { for (Enumeration en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("Exception", ex.toString()); } return "127.0.0.1"; } }
转载地址:http://ogsgi.baihongyu.com/