欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断网络

程序员文章站 2022-06-05 20:39:30
...
  1. public static final int NETTYPE_WIFI = 0x01;
  2. public static final int NETTYPE_CMWAP = 0x02;
  3. public static final int NETTYPE_CMNET = 0x03;
  4. public int getNetworkType() {
  5. int netType = 0;
  6. ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  7. NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  8. if (networkInfo == null) {
  9. return netType;
  10. }
  11. int nType = networkInfo.getType();
  12. if (nType == ConnectivityManager.TYPE_MOBILE) {
  13. String extraInfo = networkInfo.getExtraInfo();
  14. if(extraInfo!=null){
  15. if (extraInfo.toLowerCase().equals("cmnet")) {
  16. netType = NETTYPE_CMNET;
  17. } else {
  18. netType = NETTYPE_CMWAP;
  19. }
  20. }
  21. } else if (nType == ConnectivityManager.TYPE_WIFI) {
  22. netType = NETTYPE_WIFI;
  23. }
  24. return netType;
  25. }