8a22d286e3a57475c7d9b2fafc5d835da37db2ea.svn-base 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.example.myapplication.utils;
  2. import android.content.Context;
  3. import android.net.ConnectivityManager;
  4. import android.net.NetworkInfo;
  5. import android.provider.Settings;
  6. /**
  7. * <h2>Common phone utility methods</h2> <p/> <h3>Common uses:</h3> <code>PhoneUtils.{@link
  8. * #isRotationEnabled isRotationEnabled}(this);</code> <br /> <code>PhoneUtils.{@link
  9. * #isNetworkAvailable isNetworkAvailable}(this);</code> <br /> <code>PhoneUtils.{@link
  10. * #isConnectedWifi isConnectedWifi}(this);</code><br /> <code>PhoneUtils.{@link #isConnectedMobile
  11. * isConnectedMobile}(this);</code> <br />
  12. */
  13. public class HttpUtils {
  14. /**
  15. * Checks to see if the user has rotation enabled/disabled in their phone settings.
  16. *
  17. * @param context The current Context or Activity that this method is called from
  18. * @return true if rotation is enabled, otherwise false.
  19. */
  20. public static boolean isRotationEnabled(Context context) {
  21. return Settings.System.getInt(context.getContentResolver(),
  22. Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
  23. }
  24. /**
  25. * Checks to see if the device is connected to a network (cell, wifi, etc).
  26. *
  27. * @param context The current Context or Activity that this method is called from
  28. * @return true if a network connection is available, otherwise false.
  29. */
  30. public static boolean isNetworkAvailable(Context context) {
  31. ConnectivityManager connectivityManager = (ConnectivityManager) context
  32. .getSystemService(Context.CONNECTIVITY_SERVICE);
  33. NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  34. return activeNetworkInfo != null && activeNetworkInfo.isConnected();
  35. }
  36. /**
  37. * Check if there is any connectivity to a Wifi network. <p/> Can be used in combination with
  38. * {@link #isConnectedMobile} to provide different features if the device is on a wifi network
  39. * or a cell network.
  40. *
  41. * @param context The current Context or Activity that this method is called from
  42. * @return true if a wifi connection is available, otherwise false.
  43. */
  44. public static boolean isConnectedWifi(Context context) {
  45. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  46. NetworkInfo info = cm.getActiveNetworkInfo();
  47. return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
  48. }
  49. /**
  50. * Check if there is any connectivity to a mobile network <p/> Can be used in combination with
  51. * {@link #isConnectedWifi} to provide different features if the device is on a wifi network or
  52. * a cell network.
  53. *
  54. * @param context The current Context or Activity that this method is called from
  55. * @return true if a mobile connection is available, otherwise false.
  56. */
  57. public static boolean isConnectedMobile(Context context) {
  58. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  59. NetworkInfo info = cm.getActiveNetworkInfo();
  60. return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
  61. }
  62. }