当前位置:   article > 正文

网络状态检查工具类_networkinfo info = getactivenetworkinfo();

networkinfo info = getactivenetworkinfo();

 网络状态检查工具类,做个记录方便以后使用

  1. package com.wjy.myokhttp.net;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.net.ConnectivityManager;
  5. import android.net.NetworkInfo;
  6. import android.net.wifi.WifiInfo;
  7. import android.net.wifi.WifiManager;
  8. import android.telephony.TelephonyManager;
  9. import com.wjy.myokhttp.application.MyApplication;
  10. /***********************************************************************
  11. * @creator : WangJinyong
  12. * @create-time : 2019/11/28
  13. * @description : 网络状态检查工具类
  14. ***********************************************************************/
  15. public class NetWorkUtil {
  16. private NetWorkUtil() {
  17. throw new UnsupportedOperationException("");
  18. }
  19. public static final int NETWORK_WIFI = 1; // wifi network
  20. public static final int NETWORK_4G = 4; // "4G" networks
  21. public static final int NETWORK_3G = 3; // "3G" networks
  22. public static final int NETWORK_2G = 2; // "2G" networks
  23. public static final int NETWORK_UNKNOWN = 5; // unknown network
  24. public static final int NETWORK_NO = -1; // no network
  25. private static final int NETWORK_TYPE_GSM = 16;
  26. private static final int NETWORK_TYPE_TD_SCDMA = 17;
  27. private static final int NETWORK_TYPE_IWLAN = 18;
  28. /**
  29. * 打开网络设置界面
  30. * <p>3.0以下打开设置界面</p>
  31. *
  32. * @param context 上下文
  33. */
  34. public static void openWirelessSettings(Context context) {
  35. if (android.os.Build.VERSION.SDK_INT > 10) {
  36. context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
  37. } else {
  38. context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
  39. }
  40. }
  41. /**
  42. * 获取活动网络信息
  43. *
  44. * @param context 上下文
  45. * @return NetworkInfo
  46. */
  47. private static NetworkInfo getActiveNetworkInfo(Context context) {
  48. ConnectivityManager cm = (ConnectivityManager) context
  49. .getSystemService(Context.CONNECTIVITY_SERVICE);
  50. return cm.getActiveNetworkInfo();
  51. }
  52. /**
  53. * 判断网络是否可用
  54. * <p>需添加权限 {@code <uses-permission android:name="android.permission
  55. * .ACCESS_NETWORK_STATE"/>}</p>
  56. *
  57. * @param context 上下文
  58. * @return {@code true}: 可用<br>{@code false}: 不可用
  59. */
  60. public static boolean isAvailable(Context context) {
  61. NetworkInfo info = getActiveNetworkInfo(context);
  62. return info != null && info.isAvailable();
  63. }
  64. /**
  65. * 判断网络是否连接
  66. * <p>需添加权限 {@code <uses-permission android:name="android.permission
  67. * .ACCESS_NETWORK_STATE"/>}</p>
  68. *
  69. * @param context 上下文
  70. * @return {@code true}: 是<br>{@code false}: 否
  71. */
  72. public static boolean isConnected(Context context) {
  73. NetworkInfo info = getActiveNetworkInfo(context);
  74. return info != null && info.isConnected();
  75. }
  76. /**
  77. * 判断是否有网络连接
  78. *
  79. * @return
  80. */
  81. public static boolean isNetworkConnected() {
  82. ConnectivityManager mConnectivityManager = (ConnectivityManager) MyApplication.context
  83. .getSystemService(Context.CONNECTIVITY_SERVICE);
  84. NetworkInfo mNetworkInfo = mConnectivityManager
  85. .getActiveNetworkInfo();
  86. if (mNetworkInfo != null) {
  87. return mNetworkInfo.isAvailable();
  88. }
  89. return false;
  90. }
  91. /**
  92. * 判断网络是否是4G
  93. * <p>需添加权限 {@code <uses-permission android:name="android.permission
  94. * .ACCESS_NETWORK_STATE"/>}</p>
  95. *
  96. * @param context 上下文
  97. * @return {@code true}: 是<br>{@code false}: 不是
  98. */
  99. public static boolean is4G(Context context) {
  100. NetworkInfo info = getActiveNetworkInfo(context);
  101. return info != null && info.isAvailable() && info.getSubtype() == TelephonyManager
  102. .NETWORK_TYPE_LTE;
  103. }
  104. /**
  105. * 判断wifi是否连接状态
  106. * <p>需添加权限 {@code <uses-permission android:name="android.permission
  107. * .ACCESS_NETWORK_STATE"/>}</p>
  108. *
  109. * @param context 上下文
  110. * @return {@code true}: 连接<br>{@code false}: 未连接
  111. */
  112. public static boolean isWifiConnected(Context context) {
  113. ConnectivityManager cm = (ConnectivityManager) context
  114. .getSystemService(Context.CONNECTIVITY_SERVICE);
  115. return cm != null && cm.getActiveNetworkInfo() != null
  116. && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
  117. }
  118. /**
  119. * 获取移动网络运营商名称
  120. * <p>如中国联通、中国移动、中国电信</p>
  121. *
  122. * @param context 上下文
  123. * @return 移动网络运营商名称
  124. */
  125. public static String getNetworkOperatorName(Context context) {
  126. TelephonyManager tm = (TelephonyManager) context
  127. .getSystemService(Context.TELEPHONY_SERVICE);
  128. return tm != null ? tm.getNetworkOperatorName() : null;
  129. }
  130. /**
  131. * 获取移动终端类型
  132. *
  133. * @param context 上下文
  134. * @return 手机制式
  135. * <ul>
  136. * <li>{@link TelephonyManager#PHONE_TYPE_NONE } : 0 手机制式未知</li>
  137. * <li>{@link TelephonyManager#PHONE_TYPE_GSM } : 1 手机制式为GSM,移动和联通</li>
  138. * <li>{@link TelephonyManager#PHONE_TYPE_CDMA } : 2 手机制式为CDMA,电信</li>
  139. * <li>{@link TelephonyManager#PHONE_TYPE_SIP } : 3</li>
  140. * </ul>
  141. */
  142. public static int getPhoneType(Context context) {
  143. TelephonyManager tm = (TelephonyManager) context
  144. .getSystemService(Context.TELEPHONY_SERVICE);
  145. return tm != null ? tm.getPhoneType() : -1;
  146. }
  147. /**
  148. * 获取当前的网络类型(WIFI,2G,3G,4G)
  149. * <p>需添加权限 {@code <uses-permission android:name="android.permission
  150. * .ACCESS_NETWORK_STATE"/>}</p>
  151. *
  152. * @param context 上下文
  153. * @return 网络类型
  154. * <ul>
  155. * <li>{@link #NETWORK_WIFI } = 1;</li>
  156. * <li>{@link #NETWORK_4G } = 4;</li>
  157. * <li>{@link #NETWORK_3G } = 3;</li>
  158. * <li>{@link #NETWORK_2G } = 2;</li>
  159. * <li>{@link #NETWORK_UNKNOWN} = 5;</li>
  160. * <li>{@link #NETWORK_NO } = -1;</li>
  161. * </ul>
  162. */
  163. public static int getNetWorkType(Context context) {
  164. int netType = NETWORK_NO;
  165. NetworkInfo info = getActiveNetworkInfo(context);
  166. if (info != null && info.isAvailable()) {
  167. if (info.getType() == ConnectivityManager.TYPE_WIFI) {
  168. netType = NETWORK_WIFI;
  169. } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
  170. switch (info.getSubtype()) {
  171. case NETWORK_TYPE_GSM:
  172. case TelephonyManager.NETWORK_TYPE_GPRS:
  173. case TelephonyManager.NETWORK_TYPE_CDMA:
  174. case TelephonyManager.NETWORK_TYPE_EDGE:
  175. case TelephonyManager.NETWORK_TYPE_1xRTT:
  176. case TelephonyManager.NETWORK_TYPE_IDEN:
  177. netType = NETWORK_2G;
  178. break;
  179. case NETWORK_TYPE_TD_SCDMA:
  180. case TelephonyManager.NETWORK_TYPE_EVDO_A:
  181. case TelephonyManager.NETWORK_TYPE_UMTS:
  182. case TelephonyManager.NETWORK_TYPE_EVDO_0:
  183. case TelephonyManager.NETWORK_TYPE_HSDPA:
  184. case TelephonyManager.NETWORK_TYPE_HSUPA:
  185. case TelephonyManager.NETWORK_TYPE_HSPA:
  186. case TelephonyManager.NETWORK_TYPE_EVDO_B:
  187. case TelephonyManager.NETWORK_TYPE_EHRPD:
  188. case TelephonyManager.NETWORK_TYPE_HSPAP:
  189. netType = NETWORK_3G;
  190. break;
  191. case NETWORK_TYPE_IWLAN:
  192. case TelephonyManager.NETWORK_TYPE_LTE:
  193. netType = NETWORK_4G;
  194. break;
  195. default:
  196. String subtypeName = info.getSubtypeName();
  197. if (subtypeName.equalsIgnoreCase("TD-SCDMA")
  198. || subtypeName.equalsIgnoreCase("WCDMA")
  199. || subtypeName.equalsIgnoreCase("CDMA2000")) {
  200. netType = NETWORK_3G;
  201. } else {
  202. netType = NETWORK_UNKNOWN;
  203. }
  204. break;
  205. }
  206. } else {
  207. netType = NETWORK_UNKNOWN;
  208. }
  209. }
  210. return netType;
  211. }
  212. /**
  213. * 获取当前的网络类型(WIFI,2G,3G,4G)
  214. * <p>依赖上面的方法</p>
  215. *
  216. * @param context 上下文
  217. * @return 网络类型名称
  218. * <ul>
  219. * <li>NETWORK_WIFI </li>
  220. * <li>NETWORK_4G </li>
  221. * <li>NETWORK_3G </li>
  222. * <li>NETWORK_2G </li>
  223. * <li>NETWORK_UNKNOWN</li>
  224. * <li>NETWORK_NO </li>
  225. * </ul>
  226. */
  227. public static String getNetWorkTypeName(Context context) {
  228. switch (getNetWorkType(context)) {
  229. case NETWORK_WIFI:
  230. return "NETWORK_WIFI";
  231. case NETWORK_4G:
  232. return "NETWORK_4G";
  233. case NETWORK_3G:
  234. return "NETWORK_3G";
  235. case NETWORK_2G:
  236. return "NETWORK_2G";
  237. case NETWORK_NO:
  238. return "NETWORK_NO";
  239. default:
  240. return "NETWORK_UNKNOWN";
  241. }
  242. }
  243. /**
  244. * 获取当前连接wifi的名称
  245. *
  246. * @return
  247. */
  248. public static String getConnectWifiSsid(Context context) {
  249. if (isWifiConnected(context)) {
  250. WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  251. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  252. return wifiInfo.getSSID();
  253. }
  254. return null;
  255. }
  256. /**
  257. * 获取当前连接wifi的名称
  258. *
  259. * @return
  260. */
  261. public static String getConnectWifiIp(Context context) {
  262. if (isWifiConnected(context)) {
  263. WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  264. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  265. int ipAddress = wifiInfo.getIpAddress();
  266. if (ipAddress == 0) {
  267. return null;
  268. }
  269. return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
  270. + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
  271. }
  272. return null;
  273. }
  274. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/576803
推荐阅读
相关标签
  

闽ICP备14008679号