package com.anyun.exam.lib.util;
|
|
import android.content.Context;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
import android.net.NetworkInfo.State;
|
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiManager;
|
import android.telephony.TelephonyManager;
|
|
import java.net.Inet4Address;
|
import java.net.InetAddress;
|
import java.net.NetworkInterface;
|
import java.net.SocketException;
|
import java.util.Enumeration;
|
|
/**
|
* Created by YY on 2017/10/27.
|
*/
|
public class NetUtils {
|
/**
|
* 判断网络情况
|
*
|
* @param context 上下文
|
* @return false 表示没有网络 true 表示有网络
|
*/
|
public static boolean isNetworkAvalible(Context context) {
|
// 获得网络状态管理器
|
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
if (connectivityManager == null) {
|
return false;
|
} else {
|
// 建立网络数组
|
NetworkInfo[] net_info = connectivityManager.getAllNetworkInfo();
|
|
if (net_info != null) {
|
for (int i = 0; i < net_info.length; i++) {
|
// 判断获得的网络状态是否是处于连接状态
|
if (net_info[i].getState() == State.CONNECTED) {
|
return true;
|
}
|
}
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 判断网络是否连接
|
**/
|
public static boolean netState(Context context) {
|
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
// 获取代表联网状态的NetWorkInfo对象
|
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
|
// 获取当前的网络连接是否可用
|
boolean available = false;
|
try {
|
available = networkInfo.isAvailable();
|
} catch (Exception e) {
|
// e.printStackTrace();
|
return false;
|
}
|
if (available) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
/**
|
* 在连接到网络基础之上,判断设备是否是SIM网络连接
|
*
|
* @param context
|
* @return
|
*/
|
public static boolean IsMobileNetConnect(Context context) {
|
try {
|
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
|
if (State.CONNECTED == state)
|
return true;
|
else
|
return false;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return false;
|
}
|
}
|
|
//返回值 -1:没有网络 1:WIFI网络2:wap网络3:net网络
|
public static int GetNetype(Context context) {
|
int netType = -1;
|
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
|
if(networkInfo==null) {
|
return netType;
|
}
|
int nType = networkInfo.getType();
|
if(nType== ConnectivityManager.TYPE_MOBILE) {
|
if(networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
|
netType = 3;
|
}
|
else {
|
netType = 2;
|
}
|
}
|
else if(nType== ConnectivityManager.TYPE_WIFI) {
|
netType = 1;
|
}
|
return netType;
|
}
|
|
public static boolean hasSimCard(Context context) {
|
TelephonyManager telMgr = (TelephonyManager)
|
context.getSystemService(Context.TELEPHONY_SERVICE);
|
boolean result = true;
|
switch (telMgr.getSimState()) {
|
case TelephonyManager.SIM_STATE_ABSENT:
|
result = false; // 没有SIM卡
|
break;
|
case TelephonyManager.SIM_STATE_UNKNOWN:
|
result = false;
|
break;
|
}
|
return result;
|
}
|
|
public static TelephonyManager getTelephonyManager(Context context) {
|
// 获取telephony系统服务,用于取得SIM卡和网络相关信息
|
TelephonyManager mTelephonyManager = (TelephonyManager) context
|
.getSystemService(Context.TELEPHONY_SERVICE);
|
return mTelephonyManager;
|
}
|
|
public static String getIccid(Context context) {
|
TelephonyManager mTelephonyManager = (TelephonyManager) context
|
.getSystemService(Context.TELEPHONY_SERVICE);
|
try {
|
return mTelephonyManager.getSimSerialNumber();
|
} catch (SecurityException e) {
|
|
}
|
return null;
|
}
|
|
/**
|
* 唯一的设备ID: GSM手机的 IMEI 和 CDMA手机的 MEID. Return null if device ID is not
|
* 取得手机IMEI
|
* available.
|
*/
|
public static String getDeviceId(Context context) {
|
String mDeviceId = getTelephonyManager(context).getImei();// String
|
return mDeviceId;
|
}
|
|
/**
|
* 取得IMEI SV
|
* 设备的软件版本号: 返回移动终端的软件版本,例如:GSM手机的IMEI/SV码。 例如:the IMEI/SV(software version)
|
* for GSM phones. Return null if the software version is not available.
|
*/
|
public static String getDeviceSoftwareVersion(Context context) {
|
String mDeviceSoftwareVersion = getTelephonyManager(context).getDeviceSoftwareVersion();// String
|
return mDeviceSoftwareVersion;
|
}
|
|
/**
|
* 取得手机IMSI
|
* 返回用户唯一标识,比如GSM网络的IMSI编号 唯一的用户ID: 例如:IMSI(国际移动用户识别码) for a GSM phone.
|
* 需要权限:READ_PHONE_STATE
|
*/
|
public static String getSubscriberId(Context context) {
|
String mSubscriberId = getTelephonyManager(context).getSubscriberId();// String
|
return mSubscriberId;
|
}
|
|
public static String getIPAddress(Context context) {
|
NetworkInfo info = ((ConnectivityManager) context
|
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
|
if (info != null && info.isConnected()) {
|
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
|
try {
|
//Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
|
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
|
NetworkInterface intf = en.nextElement();
|
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
|
InetAddress inetAddress = enumIpAddr.nextElement();
|
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
|
return inetAddress.getHostAddress();
|
}
|
}
|
}
|
} catch (SocketException e) {
|
e.printStackTrace();
|
}
|
|
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
|
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
|
return ipAddress;
|
}
|
} else {
|
//当前无网络连接,请在设置中打开网络
|
}
|
return null;
|
}
|
|
/**
|
* 将得到的int类型的IP转换为String类型
|
*
|
* @param ip
|
* @return
|
*/
|
public static String intIP2StringIP(int ip) {
|
return (ip & 0xFF) + "." +
|
((ip >> 8) & 0xFF) + "." +
|
((ip >> 16) & 0xFF) + "." +
|
(ip >> 24 & 0xFF);
|
}
|
}
|