Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. package com.emil.android.util;
  2.  
  3. import android.content.Context;
  4. import android.net.ConnectivityManager;
  5. import android.net.NetworkInfo;
  6. import android.telephony.TelephonyManager;
  7.  
  8. /**
  9. * Check device's network connectivity and speed
  10. * @author emil http://stackoverflow.com/users/220710/emil
  11. *
  12. */
  13. public class Connectivity {
  14.  
  15. /**
  16. * Get the network info
  17. * @param context
  18. * @return
  19. */
  20. public static NetworkInfo getNetworkInfo(Context context){
  21. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  22. return cm.getActiveNetworkInfo();
  23. }
  24.  
  25. /**
  26. * Check if there is any connectivity
  27. * @param context
  28. * @return
  29. */
  30. public static boolean isConnected(Context context){
  31. NetworkInfo info = Connectivity.getNetworkInfo(context);
  32. return (info != null && info.isConnected());
  33. }
  34.  
  35. /**
  36. * Check if there is any connectivity to a Wifi network
  37. * @param context
  38. * @param type
  39. * @return
  40. */
  41. public static boolean isConnectedWifi(Context context){
  42. NetworkInfo info = Connectivity.getNetworkInfo(context);
  43. return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
  44. }
  45.  
  46. /**
  47. * Check if there is any connectivity to a mobile network
  48. * @param context
  49. * @param type
  50. * @return
  51. */
  52. public static boolean isConnectedMobile(Context context){
  53. NetworkInfo info = Connectivity.getNetworkInfo(context);
  54. return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
  55. }
  56.  
  57. /**
  58. * Check if there is fast connectivity
  59. * @param context
  60. * @return
  61. */
  62. public static boolean isConnectedFast(Context context){
  63. NetworkInfo info = Connectivity.getNetworkInfo(context);
  64. return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
  65. }
  66.  
  67. /**
  68. * Check if the connection is fast
  69. * @param type
  70. * @param subType
  71. * @return
  72. */
  73. public static boolean isConnectionFast(int type, int subType){
  74. if(type==ConnectivityManager.TYPE_WIFI){
  75. return true;
  76. }else if(type==ConnectivityManager.TYPE_MOBILE){
  77. switch(subType){
  78. case TelephonyManager.NETWORK_TYPE_1xRTT:
  79. return false; // ~ 50-100 kbps
  80. case TelephonyManager.NETWORK_TYPE_CDMA:
  81. return false; // ~ 14-64 kbps
  82. case TelephonyManager.NETWORK_TYPE_EDGE:
  83. return false; // ~ 50-100 kbps
  84. case TelephonyManager.NETWORK_TYPE_EVDO_0:
  85. return true; // ~ 400-1000 kbps
  86. case TelephonyManager.NETWORK_TYPE_EVDO_A:
  87. return true; // ~ 600-1400 kbps
  88. case TelephonyManager.NETWORK_TYPE_GPRS:
  89. return false; // ~ 100 kbps
  90. case TelephonyManager.NETWORK_TYPE_HSDPA:
  91. return true; // ~ 2-14 Mbps
  92. case TelephonyManager.NETWORK_TYPE_HSPA:
  93. return true; // ~ 700-1700 kbps
  94. case TelephonyManager.NETWORK_TYPE_HSUPA:
  95. return true; // ~ 1-23 Mbps
  96. case TelephonyManager.NETWORK_TYPE_UMTS:
  97. return true; // ~ 400-7000 kbps
  98. /*
  99. * Above API level 7, make sure to set android:targetSdkVersion
  100. * to appropriate level to use these
  101. */
  102. case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
  103. return true; // ~ 1-2 Mbps
  104. case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
  105. return true; // ~ 5 Mbps
  106. case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
  107. return true; // ~ 10-20 Mbps
  108. case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
  109. return false; // ~25 kbps
  110. case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
  111. return true; // ~ 10+ Mbps
  112. // Unknown
  113. case TelephonyManager.NETWORK_TYPE_UNKNOWN:
  114. default:
  115. return false;
  116. }
  117. }else{
  118. return false;
  119. }
  120. }
  121.  
  122. }
  123.  
  124. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement