Get Network type












8















I've been trying to retrive the current network type, but no success



when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN or NETWORK_TYPE_UMTS.. and so on..



i tried to use:



NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();


or



NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo 
(ConnectivityManager.TYPE_MOBILE);


but no success..



i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..










share|improve this question

























  • Can you select answer from following? So people reach to this thread can know the accepted answer.

    – Mr. Sajid Shaikh
    Nov 25 '15 at 8:46
















8















I've been trying to retrive the current network type, but no success



when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN or NETWORK_TYPE_UMTS.. and so on..



i tried to use:



NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();


or



NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo 
(ConnectivityManager.TYPE_MOBILE);


but no success..



i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..










share|improve this question

























  • Can you select answer from following? So people reach to this thread can know the accepted answer.

    – Mr. Sajid Shaikh
    Nov 25 '15 at 8:46














8












8








8


9






I've been trying to retrive the current network type, but no success



when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN or NETWORK_TYPE_UMTS.. and so on..



i tried to use:



NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();


or



NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo 
(ConnectivityManager.TYPE_MOBILE);


but no success..



i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..










share|improve this question
















I've been trying to retrive the current network type, but no success



when i say network type: i refer to know this info:
if the type is: NETWORK_TYPE_IDEN or NETWORK_TYPE_UMTS.. and so on..



i tried to use:



NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();


or



NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo 
(ConnectivityManager.TYPE_MOBILE);


but no success..



i am doing this coz i wanna know if the current network is IDEN, or if the current network is connected through wifi..







android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 18 '11 at 9:38









phihag

196k47362407




196k47362407










asked May 27 '10 at 8:05









MoshikMoshik

2791319




2791319













  • Can you select answer from following? So people reach to this thread can know the accepted answer.

    – Mr. Sajid Shaikh
    Nov 25 '15 at 8:46



















  • Can you select answer from following? So people reach to this thread can know the accepted answer.

    – Mr. Sajid Shaikh
    Nov 25 '15 at 8:46

















Can you select answer from following? So people reach to this thread can know the accepted answer.

– Mr. Sajid Shaikh
Nov 25 '15 at 8:46





Can you select answer from following? So people reach to this thread can know the accepted answer.

– Mr. Sajid Shaikh
Nov 25 '15 at 8:46












11 Answers
11






active

oldest

votes


















16














I hate magic numbers :



/**
* You need to add:
*
* <pre>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* </pre>
*
* in your AndroidManifest.xml.
*/
private String networkType() {
TelephonyManager teleMan = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
}
throw new RuntimeException("New type of network");
}





share|improve this answer


























  • RuntimeException is a crash; I prefer default: return "Unknown new type";. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.

    – 18446744073709551615
    Sep 6 '14 at 7:18











  • @18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with NETWORK_TYPE_UNKNOWN (by the name of it).

    – Mr_and_Mrs_D
    Sep 6 '14 at 9:48











  • OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the throw new RuntimeException() above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.

    – 18446744073709551615
    Sep 6 '14 at 10:43











  • @18446744073709551615: throw NewNetTypeException("Network from Mars: " + networkType) - NewNetTypeException being checked (private String networkType() throws NewNetTypeException { /*...*/} - let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.

    – Mr_and_Mrs_D
    Sep 6 '14 at 11:22













  • Upvoted for "I hate magic numbers"!

    – Luis Mendo
    Apr 26 '18 at 16:46





















15














This works for me to check the network type...



TelephonyManager teleMan =  
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int networkType = teleMan.getNetworkType();

switch (networkType)
{
case 7:
textV1.setText("1xRTT");
break;
case 4:
textV1.setText("CDMA");
break;
case 2:
textV1.setText("EDGE");
break;
case 14:
textV1.setText("eHRPD");
break;
case 5:
textV1.setText("EVDO rev. 0");
break;
case 6:
textV1.setText("EVDO rev. A");
break;
case 12:
textV1.setText("EVDO rev. B");
break;
case 1:
textV1.setText("GPRS");
break;
case 8:
textV1.setText("HSDPA");
break;
case 10:
textV1.setText("HSPA");
break;
case 15:
textV1.setText("HSPA+");
break;
case 9:
textV1.setText("HSUPA");
break;
case 11:
textV1.setText("iDen");
break;
case 13:
textV1.setText("LTE");
break;
case 3:
textV1.setText("UMTS");
break;
case 0:
textV1.setText("Unknown");
break;
}





share|improve this answer

































    14














    To get the network type (I think your talking about wifi or mobile) you can use this code snippet:



    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    //mobile
    State mobile = conMan.getNetworkInfo(0).getState();

    //wifi
    State wifi = conMan.getNetworkInfo(1).getState();


    and then use it like that:



    if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
    //mobile
    } else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
    //wifi
    }


    To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName






    share|improve this answer


























    • Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?

      – Moshik
      May 27 '10 at 8:25











    • i editet my ansewr

      – RoflcoptrException
      May 27 '10 at 8:31











    • It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?

      – Moshik
      May 31 '10 at 6:45





















    7














    I am using this function:



    public String get_network()
    {Activity act=(Activity)context;
    String network_type="UNKNOWN";//maybe usb reverse tethering
    NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (active_network!=null && active_network.isConnectedOrConnecting())
    {if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
    {network_type="WIFI";
    }
    else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
    {network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
    }
    }
    return network_type;
    }





    share|improve this answer































      2














      Best answer



      public static String getNetworkType(Context context) {

      TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
      int networkType = teleMan.getNetworkType();
      switch ( networkType ) {
      case TelephonyManager.NETWORK_TYPE_1xRTT:
      return "1xRTT";
      case TelephonyManager.NETWORK_TYPE_CDMA:
      return "CDMA";
      case TelephonyManager.NETWORK_TYPE_EDGE:
      return "EDGE";
      case TelephonyManager.NETWORK_TYPE_EHRPD:
      return "eHRPD";
      case TelephonyManager.NETWORK_TYPE_EVDO_0:
      return "EVDO rev. 0";
      case TelephonyManager.NETWORK_TYPE_EVDO_A:
      return "EVDO rev. A";
      case TelephonyManager.NETWORK_TYPE_EVDO_B:
      return "EVDO rev. B";
      case TelephonyManager.NETWORK_TYPE_GPRS:
      return "GPRS";
      case TelephonyManager.NETWORK_TYPE_HSDPA:
      return "HSDPA";
      case TelephonyManager.NETWORK_TYPE_HSPA:
      return "HSPA";
      case TelephonyManager.NETWORK_TYPE_HSPAP:
      return "HSPA+";
      case TelephonyManager.NETWORK_TYPE_HSUPA:
      return "HSUPA";
      case TelephonyManager.NETWORK_TYPE_IDEN:
      return "iDen";
      case TelephonyManager.NETWORK_TYPE_LTE:
      return "LTE";
      case TelephonyManager.NETWORK_TYPE_UMTS:
      return "UMTS";
      case TelephonyManager.NETWORK_TYPE_UNKNOWN:
      return "Unknown";
      }
      return "New type of network";
      }





      share|improve this answer































        2














        /** Network type is unknown */
        public static final int NETWORK_TYPE_UNKNOWN = 0;
        /** Current network is GPRS */
        public static final int NETWORK_TYPE_GPRS = 1;
        /** Current network is EDGE */
        public static final int NETWORK_TYPE_EDGE = 2;
        /** Current network is UMTS */
        public static final int NETWORK_TYPE_UMTS = 3;
        /** Current network is CDMA: Either IS95A or IS95B*/
        public static final int NETWORK_TYPE_CDMA = 4;
        /** Current network is EVDO revision 0*/
        public static final int NETWORK_TYPE_EVDO_0 = 5;
        /** Current network is EVDO revision A*/
        public static final int NETWORK_TYPE_EVDO_A = 6;
        /** Current network is 1xRTT*/
        public static final int NETWORK_TYPE_1xRTT = 7;
        /** Current network is HSDPA */
        public static final int NETWORK_TYPE_HSDPA = 8;
        /** Current network is HSUPA */
        public static final int NETWORK_TYPE_HSUPA = 9;
        /** Current network is HSPA */
        public static final int NETWORK_TYPE_HSPA = 10;
        /** Current network is iDen */
        public static final int NETWORK_TYPE_IDEN = 11;
        /** Current network is EVDO revision B*/
        public static final int NETWORK_TYPE_EVDO_B = 12;
        /** Current network is LTE */
        public static final int NETWORK_TYPE_LTE = 13;
        /** Current network is eHRPD */
        public static final int NETWORK_TYPE_EHRPD = 14;
        /** Current network is HSPA+ */
        public static final int NETWORK_TYPE_HSPAP = 15;
        /** Current network is GSM {@hide} */
        public static final int NETWORK_TYPE_GSM = 16;
        /** Current network is TD_SCDMA {@hide} */
        public static final int NETWORK_TYPE_TD_SCDMA = 17;
        /** Current network is IWLAN {@hide} */
        public static final int NETWORK_TYPE_IWLAN = 18;


        List of networkType Provided.






        share|improve this answer































          1














          Also, add below required permission in your AndroidManifest.xml.



          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



          Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.






          share|improve this answer

































            0














            In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.



            You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:



            http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html



            You can use the ConnectivityManager to determine the active wireless radio:



            http://developer.android.com/training/efficient-downloads/connectivity_patterns.html






            share|improve this answer































              0














              Better use would be:



                  NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
              if (activeNetworkInfo == null) return false;
              switch (activeNetworkInfo.getType()) {
              case ConnectivityManager.TYPE_WIFI:
              return true;
              }
              // public static final int TYPE_BLUETOOTH = 7;
              // public static final int TYPE_DUMMY = 8;
              // public static final int TYPE_ETHERNET = 9;
              // public static final int TYPE_MOBILE = 0;
              // public static final int TYPE_MOBILE_DUN = 4;
              // /** @deprecated */
              // @Deprecated
              // public static final int TYPE_MOBILE_HIPRI = 5;
              // /** @deprecated */
              // @Deprecated
              // public static final int TYPE_MOBILE_MMS = 2;
              // /** @deprecated */
              // @Deprecated
              // public static final int TYPE_MOBILE_SUPL = 3;
              // public static final int TYPE_VPN = 17;
              // public static final int TYPE_WIFI = 1;
              // public static final int TYPE_WIMAX = 6;


              Which other types you want to define and handle is up to you...



              For those wanting a string identifier, better use:



              activeNetworkInfo.getTypeName()
              activeNetworkInfo.getSubtypeName()





              share|improve this answer































                0














                //**Return type of connection to network



                public static int getNetworkType(Context context) {
                if (!isNetworkConnected(context))
                return -1;
                ConnectivityManager conMan = (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);

                //mobile
                State mobile = conMan.getNetworkInfo(0).getState();
                //wifi
                State wifi = conMan.getNetworkInfo(1).getState();

                int result = 0;

                if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
                result |= NETWORK_MOBILE;
                }

                if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
                result |= NETWORK_WIFI;
                }

                return result;
                }





                share|improve this answer































                  0














                  If you want to know which type of network it is, you can use this :



                  private String getNetworkClass() {
                  // network type
                  TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
                  int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
                  switch (networkType) {
                  case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                  return "Unknown network";
                  case TelephonyManager.NETWORK_TYPE_GSM:
                  return " GSM";
                  case TelephonyManager.NETWORK_TYPE_CDMA:
                  case TelephonyManager.NETWORK_TYPE_1xRTT:
                  case TelephonyManager.NETWORK_TYPE_IDEN:
                  return " 2G";
                  case TelephonyManager.NETWORK_TYPE_GPRS:
                  return " GPRS (2.5G)";
                  case TelephonyManager.NETWORK_TYPE_EDGE:
                  return " EDGE (2.75G)";
                  case TelephonyManager.NETWORK_TYPE_UMTS:
                  case TelephonyManager.NETWORK_TYPE_EVDO_0:
                  case TelephonyManager.NETWORK_TYPE_EVDO_A:
                  case TelephonyManager.NETWORK_TYPE_EVDO_B:
                  return " 3G";
                  case TelephonyManager.NETWORK_TYPE_HSPA:
                  case TelephonyManager.NETWORK_TYPE_HSDPA:
                  case TelephonyManager.NETWORK_TYPE_HSUPA:
                  return " H (3G+)";
                  case TelephonyManager.NETWORK_TYPE_EHRPD:
                  case TelephonyManager.NETWORK_TYPE_HSPAP:
                  case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                  return " H+ (3G++)";
                  case TelephonyManager.NETWORK_TYPE_LTE:
                  case TelephonyManager.NETWORK_TYPE_IWLAN:
                  return " 4G";
                  default:
                  return " 4G+";
                  }
                  }





                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2919414%2fget-network-type%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    11 Answers
                    11






                    active

                    oldest

                    votes








                    11 Answers
                    11






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    16














                    I hate magic numbers :



                    /**
                    * You need to add:
                    *
                    * <pre>
                    * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
                    * </pre>
                    *
                    * in your AndroidManifest.xml.
                    */
                    private String networkType() {
                    TelephonyManager teleMan = (TelephonyManager)
                    getSystemService(Context.TELEPHONY_SERVICE);
                    int networkType = teleMan.getNetworkType();
                    switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
                    case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
                    case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
                    case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
                    case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
                    case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
                    case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
                    case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
                    case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
                    case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
                    case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
                    case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
                    case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
                    case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
                    case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
                    }
                    throw new RuntimeException("New type of network");
                    }





                    share|improve this answer


























                    • RuntimeException is a crash; I prefer default: return "Unknown new type";. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.

                      – 18446744073709551615
                      Sep 6 '14 at 7:18











                    • @18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with NETWORK_TYPE_UNKNOWN (by the name of it).

                      – Mr_and_Mrs_D
                      Sep 6 '14 at 9:48











                    • OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the throw new RuntimeException() above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.

                      – 18446744073709551615
                      Sep 6 '14 at 10:43











                    • @18446744073709551615: throw NewNetTypeException("Network from Mars: " + networkType) - NewNetTypeException being checked (private String networkType() throws NewNetTypeException { /*...*/} - let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.

                      – Mr_and_Mrs_D
                      Sep 6 '14 at 11:22













                    • Upvoted for "I hate magic numbers"!

                      – Luis Mendo
                      Apr 26 '18 at 16:46


















                    16














                    I hate magic numbers :



                    /**
                    * You need to add:
                    *
                    * <pre>
                    * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
                    * </pre>
                    *
                    * in your AndroidManifest.xml.
                    */
                    private String networkType() {
                    TelephonyManager teleMan = (TelephonyManager)
                    getSystemService(Context.TELEPHONY_SERVICE);
                    int networkType = teleMan.getNetworkType();
                    switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
                    case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
                    case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
                    case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
                    case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
                    case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
                    case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
                    case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
                    case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
                    case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
                    case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
                    case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
                    case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
                    case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
                    case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
                    }
                    throw new RuntimeException("New type of network");
                    }





                    share|improve this answer


























                    • RuntimeException is a crash; I prefer default: return "Unknown new type";. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.

                      – 18446744073709551615
                      Sep 6 '14 at 7:18











                    • @18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with NETWORK_TYPE_UNKNOWN (by the name of it).

                      – Mr_and_Mrs_D
                      Sep 6 '14 at 9:48











                    • OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the throw new RuntimeException() above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.

                      – 18446744073709551615
                      Sep 6 '14 at 10:43











                    • @18446744073709551615: throw NewNetTypeException("Network from Mars: " + networkType) - NewNetTypeException being checked (private String networkType() throws NewNetTypeException { /*...*/} - let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.

                      – Mr_and_Mrs_D
                      Sep 6 '14 at 11:22













                    • Upvoted for "I hate magic numbers"!

                      – Luis Mendo
                      Apr 26 '18 at 16:46
















                    16












                    16








                    16







                    I hate magic numbers :



                    /**
                    * You need to add:
                    *
                    * <pre>
                    * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
                    * </pre>
                    *
                    * in your AndroidManifest.xml.
                    */
                    private String networkType() {
                    TelephonyManager teleMan = (TelephonyManager)
                    getSystemService(Context.TELEPHONY_SERVICE);
                    int networkType = teleMan.getNetworkType();
                    switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
                    case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
                    case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
                    case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
                    case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
                    case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
                    case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
                    case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
                    case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
                    case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
                    case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
                    case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
                    case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
                    case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
                    case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
                    }
                    throw new RuntimeException("New type of network");
                    }





                    share|improve this answer















                    I hate magic numbers :



                    /**
                    * You need to add:
                    *
                    * <pre>
                    * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
                    * </pre>
                    *
                    * in your AndroidManifest.xml.
                    */
                    private String networkType() {
                    TelephonyManager teleMan = (TelephonyManager)
                    getSystemService(Context.TELEPHONY_SERVICE);
                    int networkType = teleMan.getNetworkType();
                    switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_1xRTT: return "1xRTT";
                    case TelephonyManager.NETWORK_TYPE_CDMA: return "CDMA";
                    case TelephonyManager.NETWORK_TYPE_EDGE: return "EDGE";
                    case TelephonyManager.NETWORK_TYPE_EHRPD: return "eHRPD";
                    case TelephonyManager.NETWORK_TYPE_EVDO_0: return "EVDO rev. 0";
                    case TelephonyManager.NETWORK_TYPE_EVDO_A: return "EVDO rev. A";
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: return "EVDO rev. B";
                    case TelephonyManager.NETWORK_TYPE_GPRS: return "GPRS";
                    case TelephonyManager.NETWORK_TYPE_HSDPA: return "HSDPA";
                    case TelephonyManager.NETWORK_TYPE_HSPA: return "HSPA";
                    case TelephonyManager.NETWORK_TYPE_HSPAP: return "HSPA+";
                    case TelephonyManager.NETWORK_TYPE_HSUPA: return "HSUPA";
                    case TelephonyManager.NETWORK_TYPE_IDEN: return "iDen";
                    case TelephonyManager.NETWORK_TYPE_LTE: return "LTE";
                    case TelephonyManager.NETWORK_TYPE_UMTS: return "UMTS";
                    case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "Unknown";
                    }
                    throw new RuntimeException("New type of network");
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 11 '14 at 11:39

























                    answered Nov 13 '13 at 22:05









                    Mr_and_Mrs_DMr_and_Mrs_D

                    15.5k23121257




                    15.5k23121257













                    • RuntimeException is a crash; I prefer default: return "Unknown new type";. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.

                      – 18446744073709551615
                      Sep 6 '14 at 7:18











                    • @18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with NETWORK_TYPE_UNKNOWN (by the name of it).

                      – Mr_and_Mrs_D
                      Sep 6 '14 at 9:48











                    • OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the throw new RuntimeException() above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.

                      – 18446744073709551615
                      Sep 6 '14 at 10:43











                    • @18446744073709551615: throw NewNetTypeException("Network from Mars: " + networkType) - NewNetTypeException being checked (private String networkType() throws NewNetTypeException { /*...*/} - let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.

                      – Mr_and_Mrs_D
                      Sep 6 '14 at 11:22













                    • Upvoted for "I hate magic numbers"!

                      – Luis Mendo
                      Apr 26 '18 at 16:46





















                    • RuntimeException is a crash; I prefer default: return "Unknown new type";. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.

                      – 18446744073709551615
                      Sep 6 '14 at 7:18











                    • @18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with NETWORK_TYPE_UNKNOWN (by the name of it).

                      – Mr_and_Mrs_D
                      Sep 6 '14 at 9:48











                    • OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the throw new RuntimeException() above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.

                      – 18446744073709551615
                      Sep 6 '14 at 10:43











                    • @18446744073709551615: throw NewNetTypeException("Network from Mars: " + networkType) - NewNetTypeException being checked (private String networkType() throws NewNetTypeException { /*...*/} - let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.

                      – Mr_and_Mrs_D
                      Sep 6 '14 at 11:22













                    • Upvoted for "I hate magic numbers"!

                      – Luis Mendo
                      Apr 26 '18 at 16:46



















                    RuntimeException is a crash; I prefer default: return "Unknown new type";. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.

                    – 18446744073709551615
                    Sep 6 '14 at 7:18





                    RuntimeException is a crash; I prefer default: return "Unknown new type";. As of today, the code contains the full list of NETWORK_TYPE_xxx constants.

                    – 18446744073709551615
                    Sep 6 '14 at 7:18













                    @18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with NETWORK_TYPE_UNKNOWN (by the name of it).

                    – Mr_and_Mrs_D
                    Sep 6 '14 at 9:48





                    @18446744073709551615: Well yes - one should have a custom checked exception probably - what I meant to say with RE is that it should not happen - the library should take care of it with NETWORK_TYPE_UNKNOWN (by the name of it).

                    – Mr_and_Mrs_D
                    Sep 6 '14 at 9:48













                    OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the throw new RuntimeException() above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.

                    – 18446744073709551615
                    Sep 6 '14 at 10:43





                    OFF-TOPIC that's an interesting philosophical question: what value should getNetworkType() return when a new network type (say, NETWORK_TYPE_XYZ) appears and the phone registers in such a network? If it returns NETWORK_TYPE_XYZ, the old applications may crash (exactly like the throw new RuntimeException() above does). If they check the application manifest and return NETWORK_TYPE_UNKNOWN for applications built against the older versions of SDK, the developers will probably go crazy trying to find out why the old application does not see the new kind of network.

                    – 18446744073709551615
                    Sep 6 '14 at 10:43













                    @18446744073709551615: throw NewNetTypeException("Network from Mars: " + networkType) - NewNetTypeException being checked (private String networkType() throws NewNetTypeException { /*...*/} - let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.

                    – Mr_and_Mrs_D
                    Sep 6 '14 at 11:22







                    @18446744073709551615: throw NewNetTypeException("Network from Mars: " + networkType) - NewNetTypeException being checked (private String networkType() throws NewNetTypeException { /*...*/} - let the caller decide - perfectly on topic good Java style programming ;) Just did not want to add also an exception - it's Q&A here not Java seminars - but that is what I'd do.

                    – Mr_and_Mrs_D
                    Sep 6 '14 at 11:22















                    Upvoted for "I hate magic numbers"!

                    – Luis Mendo
                    Apr 26 '18 at 16:46







                    Upvoted for "I hate magic numbers"!

                    – Luis Mendo
                    Apr 26 '18 at 16:46















                    15














                    This works for me to check the network type...



                    TelephonyManager teleMan =  
                    (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                    int networkType = teleMan.getNetworkType();

                    switch (networkType)
                    {
                    case 7:
                    textV1.setText("1xRTT");
                    break;
                    case 4:
                    textV1.setText("CDMA");
                    break;
                    case 2:
                    textV1.setText("EDGE");
                    break;
                    case 14:
                    textV1.setText("eHRPD");
                    break;
                    case 5:
                    textV1.setText("EVDO rev. 0");
                    break;
                    case 6:
                    textV1.setText("EVDO rev. A");
                    break;
                    case 12:
                    textV1.setText("EVDO rev. B");
                    break;
                    case 1:
                    textV1.setText("GPRS");
                    break;
                    case 8:
                    textV1.setText("HSDPA");
                    break;
                    case 10:
                    textV1.setText("HSPA");
                    break;
                    case 15:
                    textV1.setText("HSPA+");
                    break;
                    case 9:
                    textV1.setText("HSUPA");
                    break;
                    case 11:
                    textV1.setText("iDen");
                    break;
                    case 13:
                    textV1.setText("LTE");
                    break;
                    case 3:
                    textV1.setText("UMTS");
                    break;
                    case 0:
                    textV1.setText("Unknown");
                    break;
                    }





                    share|improve this answer






























                      15














                      This works for me to check the network type...



                      TelephonyManager teleMan =  
                      (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                      int networkType = teleMan.getNetworkType();

                      switch (networkType)
                      {
                      case 7:
                      textV1.setText("1xRTT");
                      break;
                      case 4:
                      textV1.setText("CDMA");
                      break;
                      case 2:
                      textV1.setText("EDGE");
                      break;
                      case 14:
                      textV1.setText("eHRPD");
                      break;
                      case 5:
                      textV1.setText("EVDO rev. 0");
                      break;
                      case 6:
                      textV1.setText("EVDO rev. A");
                      break;
                      case 12:
                      textV1.setText("EVDO rev. B");
                      break;
                      case 1:
                      textV1.setText("GPRS");
                      break;
                      case 8:
                      textV1.setText("HSDPA");
                      break;
                      case 10:
                      textV1.setText("HSPA");
                      break;
                      case 15:
                      textV1.setText("HSPA+");
                      break;
                      case 9:
                      textV1.setText("HSUPA");
                      break;
                      case 11:
                      textV1.setText("iDen");
                      break;
                      case 13:
                      textV1.setText("LTE");
                      break;
                      case 3:
                      textV1.setText("UMTS");
                      break;
                      case 0:
                      textV1.setText("Unknown");
                      break;
                      }





                      share|improve this answer




























                        15












                        15








                        15







                        This works for me to check the network type...



                        TelephonyManager teleMan =  
                        (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                        int networkType = teleMan.getNetworkType();

                        switch (networkType)
                        {
                        case 7:
                        textV1.setText("1xRTT");
                        break;
                        case 4:
                        textV1.setText("CDMA");
                        break;
                        case 2:
                        textV1.setText("EDGE");
                        break;
                        case 14:
                        textV1.setText("eHRPD");
                        break;
                        case 5:
                        textV1.setText("EVDO rev. 0");
                        break;
                        case 6:
                        textV1.setText("EVDO rev. A");
                        break;
                        case 12:
                        textV1.setText("EVDO rev. B");
                        break;
                        case 1:
                        textV1.setText("GPRS");
                        break;
                        case 8:
                        textV1.setText("HSDPA");
                        break;
                        case 10:
                        textV1.setText("HSPA");
                        break;
                        case 15:
                        textV1.setText("HSPA+");
                        break;
                        case 9:
                        textV1.setText("HSUPA");
                        break;
                        case 11:
                        textV1.setText("iDen");
                        break;
                        case 13:
                        textV1.setText("LTE");
                        break;
                        case 3:
                        textV1.setText("UMTS");
                        break;
                        case 0:
                        textV1.setText("Unknown");
                        break;
                        }





                        share|improve this answer















                        This works for me to check the network type...



                        TelephonyManager teleMan =  
                        (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                        int networkType = teleMan.getNetworkType();

                        switch (networkType)
                        {
                        case 7:
                        textV1.setText("1xRTT");
                        break;
                        case 4:
                        textV1.setText("CDMA");
                        break;
                        case 2:
                        textV1.setText("EDGE");
                        break;
                        case 14:
                        textV1.setText("eHRPD");
                        break;
                        case 5:
                        textV1.setText("EVDO rev. 0");
                        break;
                        case 6:
                        textV1.setText("EVDO rev. A");
                        break;
                        case 12:
                        textV1.setText("EVDO rev. B");
                        break;
                        case 1:
                        textV1.setText("GPRS");
                        break;
                        case 8:
                        textV1.setText("HSDPA");
                        break;
                        case 10:
                        textV1.setText("HSPA");
                        break;
                        case 15:
                        textV1.setText("HSPA+");
                        break;
                        case 9:
                        textV1.setText("HSUPA");
                        break;
                        case 11:
                        textV1.setText("iDen");
                        break;
                        case 13:
                        textV1.setText("LTE");
                        break;
                        case 3:
                        textV1.setText("UMTS");
                        break;
                        case 0:
                        textV1.setText("Unknown");
                        break;
                        }






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jul 22 '11 at 1:05

























                        answered Jul 22 '11 at 0:21









                        outkkastoutkkast

                        2,8041136




                        2,8041136























                            14














                            To get the network type (I think your talking about wifi or mobile) you can use this code snippet:



                            ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

                            //mobile
                            State mobile = conMan.getNetworkInfo(0).getState();

                            //wifi
                            State wifi = conMan.getNetworkInfo(1).getState();


                            and then use it like that:



                            if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
                            //mobile
                            } else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
                            //wifi
                            }


                            To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName






                            share|improve this answer


























                            • Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?

                              – Moshik
                              May 27 '10 at 8:25











                            • i editet my ansewr

                              – RoflcoptrException
                              May 27 '10 at 8:31











                            • It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?

                              – Moshik
                              May 31 '10 at 6:45


















                            14














                            To get the network type (I think your talking about wifi or mobile) you can use this code snippet:



                            ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

                            //mobile
                            State mobile = conMan.getNetworkInfo(0).getState();

                            //wifi
                            State wifi = conMan.getNetworkInfo(1).getState();


                            and then use it like that:



                            if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
                            //mobile
                            } else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
                            //wifi
                            }


                            To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName






                            share|improve this answer


























                            • Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?

                              – Moshik
                              May 27 '10 at 8:25











                            • i editet my ansewr

                              – RoflcoptrException
                              May 27 '10 at 8:31











                            • It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?

                              – Moshik
                              May 31 '10 at 6:45
















                            14












                            14








                            14







                            To get the network type (I think your talking about wifi or mobile) you can use this code snippet:



                            ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

                            //mobile
                            State mobile = conMan.getNetworkInfo(0).getState();

                            //wifi
                            State wifi = conMan.getNetworkInfo(1).getState();


                            and then use it like that:



                            if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
                            //mobile
                            } else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
                            //wifi
                            }


                            To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName






                            share|improve this answer















                            To get the network type (I think your talking about wifi or mobile) you can use this code snippet:



                            ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

                            //mobile
                            State mobile = conMan.getNetworkInfo(0).getState();

                            //wifi
                            State wifi = conMan.getNetworkInfo(1).getState();


                            and then use it like that:



                            if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
                            //mobile
                            } else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
                            //wifi
                            }


                            To get the type of the mobile network I would try TelephonyManager#getNetworkType or NetworkInfo#getSubtypeName







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 27 '10 at 8:31

























                            answered May 27 '10 at 8:07









                            RoflcoptrExceptionRoflcoptrException

                            39.7k31139196




                            39.7k31139196













                            • Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?

                              – Moshik
                              May 27 '10 at 8:25











                            • i editet my ansewr

                              – RoflcoptrException
                              May 27 '10 at 8:31











                            • It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?

                              – Moshik
                              May 31 '10 at 6:45





















                            • Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?

                              – Moshik
                              May 27 '10 at 8:25











                            • i editet my ansewr

                              – RoflcoptrException
                              May 27 '10 at 8:31











                            • It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?

                              – Moshik
                              May 31 '10 at 6:45



















                            Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?

                            – Moshik
                            May 27 '10 at 8:25





                            Yes this is checking weather i`am on wifi or not.. and it does work! but is there also a way of checking my network type?(for example i am onto IDEN/GSM..) ?

                            – Moshik
                            May 27 '10 at 8:25













                            i editet my ansewr

                            – RoflcoptrException
                            May 27 '10 at 8:31





                            i editet my ansewr

                            – RoflcoptrException
                            May 27 '10 at 8:31













                            It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?

                            – Moshik
                            May 31 '10 at 6:45







                            It didnt work after i tried it with Wifi... it's still eneter into the first condisiton.. seems like NetworkInfo.State.CONNECTED always return true.. any idea?

                            – Moshik
                            May 31 '10 at 6:45













                            7














                            I am using this function:



                            public String get_network()
                            {Activity act=(Activity)context;
                            String network_type="UNKNOWN";//maybe usb reverse tethering
                            NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                            if (active_network!=null && active_network.isConnectedOrConnecting())
                            {if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
                            {network_type="WIFI";
                            }
                            else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
                            {network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
                            }
                            }
                            return network_type;
                            }





                            share|improve this answer




























                              7














                              I am using this function:



                              public String get_network()
                              {Activity act=(Activity)context;
                              String network_type="UNKNOWN";//maybe usb reverse tethering
                              NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                              if (active_network!=null && active_network.isConnectedOrConnecting())
                              {if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
                              {network_type="WIFI";
                              }
                              else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
                              {network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
                              }
                              }
                              return network_type;
                              }





                              share|improve this answer


























                                7












                                7








                                7







                                I am using this function:



                                public String get_network()
                                {Activity act=(Activity)context;
                                String network_type="UNKNOWN";//maybe usb reverse tethering
                                NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                                if (active_network!=null && active_network.isConnectedOrConnecting())
                                {if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
                                {network_type="WIFI";
                                }
                                else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
                                {network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
                                }
                                }
                                return network_type;
                                }





                                share|improve this answer













                                I am using this function:



                                public String get_network()
                                {Activity act=(Activity)context;
                                String network_type="UNKNOWN";//maybe usb reverse tethering
                                NetworkInfo active_network=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                                if (active_network!=null && active_network.isConnectedOrConnecting())
                                {if (active_network.getType()==ConnectivityManager.TYPE_WIFI)
                                {network_type="WIFI";
                                }
                                else if (active_network.getType()==ConnectivityManager.TYPE_MOBILE)
                                {network_type=((ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo().getSubtypeName();
                                }
                                }
                                return network_type;
                                }






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Aug 5 '11 at 10:02









                                diyismdiyism

                                8,48654037




                                8,48654037























                                    2














                                    Best answer



                                    public static String getNetworkType(Context context) {

                                    TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                                    int networkType = teleMan.getNetworkType();
                                    switch ( networkType ) {
                                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                                    return "1xRTT";
                                    case TelephonyManager.NETWORK_TYPE_CDMA:
                                    return "CDMA";
                                    case TelephonyManager.NETWORK_TYPE_EDGE:
                                    return "EDGE";
                                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                                    return "eHRPD";
                                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                    return "EVDO rev. 0";
                                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                    return "EVDO rev. A";
                                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                    return "EVDO rev. B";
                                    case TelephonyManager.NETWORK_TYPE_GPRS:
                                    return "GPRS";
                                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                                    return "HSDPA";
                                    case TelephonyManager.NETWORK_TYPE_HSPA:
                                    return "HSPA";
                                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                                    return "HSPA+";
                                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                                    return "HSUPA";
                                    case TelephonyManager.NETWORK_TYPE_IDEN:
                                    return "iDen";
                                    case TelephonyManager.NETWORK_TYPE_LTE:
                                    return "LTE";
                                    case TelephonyManager.NETWORK_TYPE_UMTS:
                                    return "UMTS";
                                    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                                    return "Unknown";
                                    }
                                    return "New type of network";
                                    }





                                    share|improve this answer




























                                      2














                                      Best answer



                                      public static String getNetworkType(Context context) {

                                      TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                                      int networkType = teleMan.getNetworkType();
                                      switch ( networkType ) {
                                      case TelephonyManager.NETWORK_TYPE_1xRTT:
                                      return "1xRTT";
                                      case TelephonyManager.NETWORK_TYPE_CDMA:
                                      return "CDMA";
                                      case TelephonyManager.NETWORK_TYPE_EDGE:
                                      return "EDGE";
                                      case TelephonyManager.NETWORK_TYPE_EHRPD:
                                      return "eHRPD";
                                      case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                      return "EVDO rev. 0";
                                      case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                      return "EVDO rev. A";
                                      case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                      return "EVDO rev. B";
                                      case TelephonyManager.NETWORK_TYPE_GPRS:
                                      return "GPRS";
                                      case TelephonyManager.NETWORK_TYPE_HSDPA:
                                      return "HSDPA";
                                      case TelephonyManager.NETWORK_TYPE_HSPA:
                                      return "HSPA";
                                      case TelephonyManager.NETWORK_TYPE_HSPAP:
                                      return "HSPA+";
                                      case TelephonyManager.NETWORK_TYPE_HSUPA:
                                      return "HSUPA";
                                      case TelephonyManager.NETWORK_TYPE_IDEN:
                                      return "iDen";
                                      case TelephonyManager.NETWORK_TYPE_LTE:
                                      return "LTE";
                                      case TelephonyManager.NETWORK_TYPE_UMTS:
                                      return "UMTS";
                                      case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                                      return "Unknown";
                                      }
                                      return "New type of network";
                                      }





                                      share|improve this answer


























                                        2












                                        2








                                        2







                                        Best answer



                                        public static String getNetworkType(Context context) {

                                        TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                                        int networkType = teleMan.getNetworkType();
                                        switch ( networkType ) {
                                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                                        return "1xRTT";
                                        case TelephonyManager.NETWORK_TYPE_CDMA:
                                        return "CDMA";
                                        case TelephonyManager.NETWORK_TYPE_EDGE:
                                        return "EDGE";
                                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                                        return "eHRPD";
                                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                        return "EVDO rev. 0";
                                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                        return "EVDO rev. A";
                                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                        return "EVDO rev. B";
                                        case TelephonyManager.NETWORK_TYPE_GPRS:
                                        return "GPRS";
                                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                                        return "HSDPA";
                                        case TelephonyManager.NETWORK_TYPE_HSPA:
                                        return "HSPA";
                                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                                        return "HSPA+";
                                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                                        return "HSUPA";
                                        case TelephonyManager.NETWORK_TYPE_IDEN:
                                        return "iDen";
                                        case TelephonyManager.NETWORK_TYPE_LTE:
                                        return "LTE";
                                        case TelephonyManager.NETWORK_TYPE_UMTS:
                                        return "UMTS";
                                        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                                        return "Unknown";
                                        }
                                        return "New type of network";
                                        }





                                        share|improve this answer













                                        Best answer



                                        public static String getNetworkType(Context context) {

                                        TelephonyManager teleMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                                        int networkType = teleMan.getNetworkType();
                                        switch ( networkType ) {
                                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                                        return "1xRTT";
                                        case TelephonyManager.NETWORK_TYPE_CDMA:
                                        return "CDMA";
                                        case TelephonyManager.NETWORK_TYPE_EDGE:
                                        return "EDGE";
                                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                                        return "eHRPD";
                                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                        return "EVDO rev. 0";
                                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                        return "EVDO rev. A";
                                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                        return "EVDO rev. B";
                                        case TelephonyManager.NETWORK_TYPE_GPRS:
                                        return "GPRS";
                                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                                        return "HSDPA";
                                        case TelephonyManager.NETWORK_TYPE_HSPA:
                                        return "HSPA";
                                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                                        return "HSPA+";
                                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                                        return "HSUPA";
                                        case TelephonyManager.NETWORK_TYPE_IDEN:
                                        return "iDen";
                                        case TelephonyManager.NETWORK_TYPE_LTE:
                                        return "LTE";
                                        case TelephonyManager.NETWORK_TYPE_UMTS:
                                        return "UMTS";
                                        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                                        return "Unknown";
                                        }
                                        return "New type of network";
                                        }






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 17 '15 at 18:05









                                        sonidasonida

                                        3,76813138




                                        3,76813138























                                            2














                                            /** Network type is unknown */
                                            public static final int NETWORK_TYPE_UNKNOWN = 0;
                                            /** Current network is GPRS */
                                            public static final int NETWORK_TYPE_GPRS = 1;
                                            /** Current network is EDGE */
                                            public static final int NETWORK_TYPE_EDGE = 2;
                                            /** Current network is UMTS */
                                            public static final int NETWORK_TYPE_UMTS = 3;
                                            /** Current network is CDMA: Either IS95A or IS95B*/
                                            public static final int NETWORK_TYPE_CDMA = 4;
                                            /** Current network is EVDO revision 0*/
                                            public static final int NETWORK_TYPE_EVDO_0 = 5;
                                            /** Current network is EVDO revision A*/
                                            public static final int NETWORK_TYPE_EVDO_A = 6;
                                            /** Current network is 1xRTT*/
                                            public static final int NETWORK_TYPE_1xRTT = 7;
                                            /** Current network is HSDPA */
                                            public static final int NETWORK_TYPE_HSDPA = 8;
                                            /** Current network is HSUPA */
                                            public static final int NETWORK_TYPE_HSUPA = 9;
                                            /** Current network is HSPA */
                                            public static final int NETWORK_TYPE_HSPA = 10;
                                            /** Current network is iDen */
                                            public static final int NETWORK_TYPE_IDEN = 11;
                                            /** Current network is EVDO revision B*/
                                            public static final int NETWORK_TYPE_EVDO_B = 12;
                                            /** Current network is LTE */
                                            public static final int NETWORK_TYPE_LTE = 13;
                                            /** Current network is eHRPD */
                                            public static final int NETWORK_TYPE_EHRPD = 14;
                                            /** Current network is HSPA+ */
                                            public static final int NETWORK_TYPE_HSPAP = 15;
                                            /** Current network is GSM {@hide} */
                                            public static final int NETWORK_TYPE_GSM = 16;
                                            /** Current network is TD_SCDMA {@hide} */
                                            public static final int NETWORK_TYPE_TD_SCDMA = 17;
                                            /** Current network is IWLAN {@hide} */
                                            public static final int NETWORK_TYPE_IWLAN = 18;


                                            List of networkType Provided.






                                            share|improve this answer




























                                              2














                                              /** Network type is unknown */
                                              public static final int NETWORK_TYPE_UNKNOWN = 0;
                                              /** Current network is GPRS */
                                              public static final int NETWORK_TYPE_GPRS = 1;
                                              /** Current network is EDGE */
                                              public static final int NETWORK_TYPE_EDGE = 2;
                                              /** Current network is UMTS */
                                              public static final int NETWORK_TYPE_UMTS = 3;
                                              /** Current network is CDMA: Either IS95A or IS95B*/
                                              public static final int NETWORK_TYPE_CDMA = 4;
                                              /** Current network is EVDO revision 0*/
                                              public static final int NETWORK_TYPE_EVDO_0 = 5;
                                              /** Current network is EVDO revision A*/
                                              public static final int NETWORK_TYPE_EVDO_A = 6;
                                              /** Current network is 1xRTT*/
                                              public static final int NETWORK_TYPE_1xRTT = 7;
                                              /** Current network is HSDPA */
                                              public static final int NETWORK_TYPE_HSDPA = 8;
                                              /** Current network is HSUPA */
                                              public static final int NETWORK_TYPE_HSUPA = 9;
                                              /** Current network is HSPA */
                                              public static final int NETWORK_TYPE_HSPA = 10;
                                              /** Current network is iDen */
                                              public static final int NETWORK_TYPE_IDEN = 11;
                                              /** Current network is EVDO revision B*/
                                              public static final int NETWORK_TYPE_EVDO_B = 12;
                                              /** Current network is LTE */
                                              public static final int NETWORK_TYPE_LTE = 13;
                                              /** Current network is eHRPD */
                                              public static final int NETWORK_TYPE_EHRPD = 14;
                                              /** Current network is HSPA+ */
                                              public static final int NETWORK_TYPE_HSPAP = 15;
                                              /** Current network is GSM {@hide} */
                                              public static final int NETWORK_TYPE_GSM = 16;
                                              /** Current network is TD_SCDMA {@hide} */
                                              public static final int NETWORK_TYPE_TD_SCDMA = 17;
                                              /** Current network is IWLAN {@hide} */
                                              public static final int NETWORK_TYPE_IWLAN = 18;


                                              List of networkType Provided.






                                              share|improve this answer


























                                                2












                                                2








                                                2







                                                /** Network type is unknown */
                                                public static final int NETWORK_TYPE_UNKNOWN = 0;
                                                /** Current network is GPRS */
                                                public static final int NETWORK_TYPE_GPRS = 1;
                                                /** Current network is EDGE */
                                                public static final int NETWORK_TYPE_EDGE = 2;
                                                /** Current network is UMTS */
                                                public static final int NETWORK_TYPE_UMTS = 3;
                                                /** Current network is CDMA: Either IS95A or IS95B*/
                                                public static final int NETWORK_TYPE_CDMA = 4;
                                                /** Current network is EVDO revision 0*/
                                                public static final int NETWORK_TYPE_EVDO_0 = 5;
                                                /** Current network is EVDO revision A*/
                                                public static final int NETWORK_TYPE_EVDO_A = 6;
                                                /** Current network is 1xRTT*/
                                                public static final int NETWORK_TYPE_1xRTT = 7;
                                                /** Current network is HSDPA */
                                                public static final int NETWORK_TYPE_HSDPA = 8;
                                                /** Current network is HSUPA */
                                                public static final int NETWORK_TYPE_HSUPA = 9;
                                                /** Current network is HSPA */
                                                public static final int NETWORK_TYPE_HSPA = 10;
                                                /** Current network is iDen */
                                                public static final int NETWORK_TYPE_IDEN = 11;
                                                /** Current network is EVDO revision B*/
                                                public static final int NETWORK_TYPE_EVDO_B = 12;
                                                /** Current network is LTE */
                                                public static final int NETWORK_TYPE_LTE = 13;
                                                /** Current network is eHRPD */
                                                public static final int NETWORK_TYPE_EHRPD = 14;
                                                /** Current network is HSPA+ */
                                                public static final int NETWORK_TYPE_HSPAP = 15;
                                                /** Current network is GSM {@hide} */
                                                public static final int NETWORK_TYPE_GSM = 16;
                                                /** Current network is TD_SCDMA {@hide} */
                                                public static final int NETWORK_TYPE_TD_SCDMA = 17;
                                                /** Current network is IWLAN {@hide} */
                                                public static final int NETWORK_TYPE_IWLAN = 18;


                                                List of networkType Provided.






                                                share|improve this answer













                                                /** Network type is unknown */
                                                public static final int NETWORK_TYPE_UNKNOWN = 0;
                                                /** Current network is GPRS */
                                                public static final int NETWORK_TYPE_GPRS = 1;
                                                /** Current network is EDGE */
                                                public static final int NETWORK_TYPE_EDGE = 2;
                                                /** Current network is UMTS */
                                                public static final int NETWORK_TYPE_UMTS = 3;
                                                /** Current network is CDMA: Either IS95A or IS95B*/
                                                public static final int NETWORK_TYPE_CDMA = 4;
                                                /** Current network is EVDO revision 0*/
                                                public static final int NETWORK_TYPE_EVDO_0 = 5;
                                                /** Current network is EVDO revision A*/
                                                public static final int NETWORK_TYPE_EVDO_A = 6;
                                                /** Current network is 1xRTT*/
                                                public static final int NETWORK_TYPE_1xRTT = 7;
                                                /** Current network is HSDPA */
                                                public static final int NETWORK_TYPE_HSDPA = 8;
                                                /** Current network is HSUPA */
                                                public static final int NETWORK_TYPE_HSUPA = 9;
                                                /** Current network is HSPA */
                                                public static final int NETWORK_TYPE_HSPA = 10;
                                                /** Current network is iDen */
                                                public static final int NETWORK_TYPE_IDEN = 11;
                                                /** Current network is EVDO revision B*/
                                                public static final int NETWORK_TYPE_EVDO_B = 12;
                                                /** Current network is LTE */
                                                public static final int NETWORK_TYPE_LTE = 13;
                                                /** Current network is eHRPD */
                                                public static final int NETWORK_TYPE_EHRPD = 14;
                                                /** Current network is HSPA+ */
                                                public static final int NETWORK_TYPE_HSPAP = 15;
                                                /** Current network is GSM {@hide} */
                                                public static final int NETWORK_TYPE_GSM = 16;
                                                /** Current network is TD_SCDMA {@hide} */
                                                public static final int NETWORK_TYPE_TD_SCDMA = 17;
                                                /** Current network is IWLAN {@hide} */
                                                public static final int NETWORK_TYPE_IWLAN = 18;


                                                List of networkType Provided.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Mar 6 '16 at 18:08









                                                Bamboo ChemistBamboo Chemist

                                                412




                                                412























                                                    1














                                                    Also, add below required permission in your AndroidManifest.xml.



                                                    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



                                                    Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.






                                                    share|improve this answer






























                                                      1














                                                      Also, add below required permission in your AndroidManifest.xml.



                                                      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



                                                      Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.






                                                      share|improve this answer




























                                                        1












                                                        1








                                                        1







                                                        Also, add below required permission in your AndroidManifest.xml.



                                                        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



                                                        Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.






                                                        share|improve this answer















                                                        Also, add below required permission in your AndroidManifest.xml.



                                                        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



                                                        Otherwise, you will face app crash while getting ConnectivityManager handle due to security exception.







                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Mar 17 '12 at 9:21

























                                                        answered Mar 17 '12 at 9:11









                                                        Krishna ShettyKrishna Shetty

                                                        51511032




                                                        51511032























                                                            0














                                                            In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.



                                                            You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:



                                                            http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html



                                                            You can use the ConnectivityManager to determine the active wireless radio:



                                                            http://developer.android.com/training/efficient-downloads/connectivity_patterns.html






                                                            share|improve this answer




























                                                              0














                                                              In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.



                                                              You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:



                                                              http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html



                                                              You can use the ConnectivityManager to determine the active wireless radio:



                                                              http://developer.android.com/training/efficient-downloads/connectivity_patterns.html






                                                              share|improve this answer


























                                                                0












                                                                0








                                                                0







                                                                In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.



                                                                You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:



                                                                http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html



                                                                You can use the ConnectivityManager to determine the active wireless radio:



                                                                http://developer.android.com/training/efficient-downloads/connectivity_patterns.html






                                                                share|improve this answer













                                                                In my experience... it's best to use Android training guides for these types of efforts. It's easy to get null pointer exceptions when you use these classes, and it's especially bad when you try to detect these connections when the app first opens and then the app crashes.



                                                                You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place:



                                                                http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html



                                                                You can use the ConnectivityManager to determine the active wireless radio:



                                                                http://developer.android.com/training/efficient-downloads/connectivity_patterns.html







                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered Jan 12 '13 at 0:14









                                                                Lou MordaLou Morda

                                                                3,21012941




                                                                3,21012941























                                                                    0














                                                                    Better use would be:



                                                                        NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                                                                    if (activeNetworkInfo == null) return false;
                                                                    switch (activeNetworkInfo.getType()) {
                                                                    case ConnectivityManager.TYPE_WIFI:
                                                                    return true;
                                                                    }
                                                                    // public static final int TYPE_BLUETOOTH = 7;
                                                                    // public static final int TYPE_DUMMY = 8;
                                                                    // public static final int TYPE_ETHERNET = 9;
                                                                    // public static final int TYPE_MOBILE = 0;
                                                                    // public static final int TYPE_MOBILE_DUN = 4;
                                                                    // /** @deprecated */
                                                                    // @Deprecated
                                                                    // public static final int TYPE_MOBILE_HIPRI = 5;
                                                                    // /** @deprecated */
                                                                    // @Deprecated
                                                                    // public static final int TYPE_MOBILE_MMS = 2;
                                                                    // /** @deprecated */
                                                                    // @Deprecated
                                                                    // public static final int TYPE_MOBILE_SUPL = 3;
                                                                    // public static final int TYPE_VPN = 17;
                                                                    // public static final int TYPE_WIFI = 1;
                                                                    // public static final int TYPE_WIMAX = 6;


                                                                    Which other types you want to define and handle is up to you...



                                                                    For those wanting a string identifier, better use:



                                                                    activeNetworkInfo.getTypeName()
                                                                    activeNetworkInfo.getSubtypeName()





                                                                    share|improve this answer




























                                                                      0














                                                                      Better use would be:



                                                                          NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                                                                      if (activeNetworkInfo == null) return false;
                                                                      switch (activeNetworkInfo.getType()) {
                                                                      case ConnectivityManager.TYPE_WIFI:
                                                                      return true;
                                                                      }
                                                                      // public static final int TYPE_BLUETOOTH = 7;
                                                                      // public static final int TYPE_DUMMY = 8;
                                                                      // public static final int TYPE_ETHERNET = 9;
                                                                      // public static final int TYPE_MOBILE = 0;
                                                                      // public static final int TYPE_MOBILE_DUN = 4;
                                                                      // /** @deprecated */
                                                                      // @Deprecated
                                                                      // public static final int TYPE_MOBILE_HIPRI = 5;
                                                                      // /** @deprecated */
                                                                      // @Deprecated
                                                                      // public static final int TYPE_MOBILE_MMS = 2;
                                                                      // /** @deprecated */
                                                                      // @Deprecated
                                                                      // public static final int TYPE_MOBILE_SUPL = 3;
                                                                      // public static final int TYPE_VPN = 17;
                                                                      // public static final int TYPE_WIFI = 1;
                                                                      // public static final int TYPE_WIMAX = 6;


                                                                      Which other types you want to define and handle is up to you...



                                                                      For those wanting a string identifier, better use:



                                                                      activeNetworkInfo.getTypeName()
                                                                      activeNetworkInfo.getSubtypeName()





                                                                      share|improve this answer


























                                                                        0












                                                                        0








                                                                        0







                                                                        Better use would be:



                                                                            NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                                                                        if (activeNetworkInfo == null) return false;
                                                                        switch (activeNetworkInfo.getType()) {
                                                                        case ConnectivityManager.TYPE_WIFI:
                                                                        return true;
                                                                        }
                                                                        // public static final int TYPE_BLUETOOTH = 7;
                                                                        // public static final int TYPE_DUMMY = 8;
                                                                        // public static final int TYPE_ETHERNET = 9;
                                                                        // public static final int TYPE_MOBILE = 0;
                                                                        // public static final int TYPE_MOBILE_DUN = 4;
                                                                        // /** @deprecated */
                                                                        // @Deprecated
                                                                        // public static final int TYPE_MOBILE_HIPRI = 5;
                                                                        // /** @deprecated */
                                                                        // @Deprecated
                                                                        // public static final int TYPE_MOBILE_MMS = 2;
                                                                        // /** @deprecated */
                                                                        // @Deprecated
                                                                        // public static final int TYPE_MOBILE_SUPL = 3;
                                                                        // public static final int TYPE_VPN = 17;
                                                                        // public static final int TYPE_WIFI = 1;
                                                                        // public static final int TYPE_WIMAX = 6;


                                                                        Which other types you want to define and handle is up to you...



                                                                        For those wanting a string identifier, better use:



                                                                        activeNetworkInfo.getTypeName()
                                                                        activeNetworkInfo.getSubtypeName()





                                                                        share|improve this answer













                                                                        Better use would be:



                                                                            NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                                                                        if (activeNetworkInfo == null) return false;
                                                                        switch (activeNetworkInfo.getType()) {
                                                                        case ConnectivityManager.TYPE_WIFI:
                                                                        return true;
                                                                        }
                                                                        // public static final int TYPE_BLUETOOTH = 7;
                                                                        // public static final int TYPE_DUMMY = 8;
                                                                        // public static final int TYPE_ETHERNET = 9;
                                                                        // public static final int TYPE_MOBILE = 0;
                                                                        // public static final int TYPE_MOBILE_DUN = 4;
                                                                        // /** @deprecated */
                                                                        // @Deprecated
                                                                        // public static final int TYPE_MOBILE_HIPRI = 5;
                                                                        // /** @deprecated */
                                                                        // @Deprecated
                                                                        // public static final int TYPE_MOBILE_MMS = 2;
                                                                        // /** @deprecated */
                                                                        // @Deprecated
                                                                        // public static final int TYPE_MOBILE_SUPL = 3;
                                                                        // public static final int TYPE_VPN = 17;
                                                                        // public static final int TYPE_WIFI = 1;
                                                                        // public static final int TYPE_WIMAX = 6;


                                                                        Which other types you want to define and handle is up to you...



                                                                        For those wanting a string identifier, better use:



                                                                        activeNetworkInfo.getTypeName()
                                                                        activeNetworkInfo.getSubtypeName()






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Sep 1 '17 at 10:04









                                                                        RickRick

                                                                        73611222




                                                                        73611222























                                                                            0














                                                                            //**Return type of connection to network



                                                                            public static int getNetworkType(Context context) {
                                                                            if (!isNetworkConnected(context))
                                                                            return -1;
                                                                            ConnectivityManager conMan = (ConnectivityManager) context.
                                                                            getSystemService(Context.CONNECTIVITY_SERVICE);

                                                                            //mobile
                                                                            State mobile = conMan.getNetworkInfo(0).getState();
                                                                            //wifi
                                                                            State wifi = conMan.getNetworkInfo(1).getState();

                                                                            int result = 0;

                                                                            if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
                                                                            result |= NETWORK_MOBILE;
                                                                            }

                                                                            if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
                                                                            result |= NETWORK_WIFI;
                                                                            }

                                                                            return result;
                                                                            }





                                                                            share|improve this answer




























                                                                              0














                                                                              //**Return type of connection to network



                                                                              public static int getNetworkType(Context context) {
                                                                              if (!isNetworkConnected(context))
                                                                              return -1;
                                                                              ConnectivityManager conMan = (ConnectivityManager) context.
                                                                              getSystemService(Context.CONNECTIVITY_SERVICE);

                                                                              //mobile
                                                                              State mobile = conMan.getNetworkInfo(0).getState();
                                                                              //wifi
                                                                              State wifi = conMan.getNetworkInfo(1).getState();

                                                                              int result = 0;

                                                                              if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
                                                                              result |= NETWORK_MOBILE;
                                                                              }

                                                                              if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
                                                                              result |= NETWORK_WIFI;
                                                                              }

                                                                              return result;
                                                                              }





                                                                              share|improve this answer


























                                                                                0












                                                                                0








                                                                                0







                                                                                //**Return type of connection to network



                                                                                public static int getNetworkType(Context context) {
                                                                                if (!isNetworkConnected(context))
                                                                                return -1;
                                                                                ConnectivityManager conMan = (ConnectivityManager) context.
                                                                                getSystemService(Context.CONNECTIVITY_SERVICE);

                                                                                //mobile
                                                                                State mobile = conMan.getNetworkInfo(0).getState();
                                                                                //wifi
                                                                                State wifi = conMan.getNetworkInfo(1).getState();

                                                                                int result = 0;

                                                                                if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
                                                                                result |= NETWORK_MOBILE;
                                                                                }

                                                                                if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
                                                                                result |= NETWORK_WIFI;
                                                                                }

                                                                                return result;
                                                                                }





                                                                                share|improve this answer













                                                                                //**Return type of connection to network



                                                                                public static int getNetworkType(Context context) {
                                                                                if (!isNetworkConnected(context))
                                                                                return -1;
                                                                                ConnectivityManager conMan = (ConnectivityManager) context.
                                                                                getSystemService(Context.CONNECTIVITY_SERVICE);

                                                                                //mobile
                                                                                State mobile = conMan.getNetworkInfo(0).getState();
                                                                                //wifi
                                                                                State wifi = conMan.getNetworkInfo(1).getState();

                                                                                int result = 0;

                                                                                if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
                                                                                result |= NETWORK_MOBILE;
                                                                                }

                                                                                if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
                                                                                result |= NETWORK_WIFI;
                                                                                }

                                                                                return result;
                                                                                }






                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Nov 1 '18 at 13:01









                                                                                Milad shoeibiMilad shoeibi

                                                                                113




                                                                                113























                                                                                    0














                                                                                    If you want to know which type of network it is, you can use this :



                                                                                    private String getNetworkClass() {
                                                                                    // network type
                                                                                    TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
                                                                                    int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
                                                                                    switch (networkType) {
                                                                                    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                                                                                    return "Unknown network";
                                                                                    case TelephonyManager.NETWORK_TYPE_GSM:
                                                                                    return " GSM";
                                                                                    case TelephonyManager.NETWORK_TYPE_CDMA:
                                                                                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                                                                                    case TelephonyManager.NETWORK_TYPE_IDEN:
                                                                                    return " 2G";
                                                                                    case TelephonyManager.NETWORK_TYPE_GPRS:
                                                                                    return " GPRS (2.5G)";
                                                                                    case TelephonyManager.NETWORK_TYPE_EDGE:
                                                                                    return " EDGE (2.75G)";
                                                                                    case TelephonyManager.NETWORK_TYPE_UMTS:
                                                                                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                                                                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                                                                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                                                                    return " 3G";
                                                                                    case TelephonyManager.NETWORK_TYPE_HSPA:
                                                                                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                                                                                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                                                                                    return " H (3G+)";
                                                                                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                                                                                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                                                                                    case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                                                                                    return " H+ (3G++)";
                                                                                    case TelephonyManager.NETWORK_TYPE_LTE:
                                                                                    case TelephonyManager.NETWORK_TYPE_IWLAN:
                                                                                    return " 4G";
                                                                                    default:
                                                                                    return " 4G+";
                                                                                    }
                                                                                    }





                                                                                    share|improve this answer




























                                                                                      0














                                                                                      If you want to know which type of network it is, you can use this :



                                                                                      private String getNetworkClass() {
                                                                                      // network type
                                                                                      TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
                                                                                      int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
                                                                                      switch (networkType) {
                                                                                      case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                                                                                      return "Unknown network";
                                                                                      case TelephonyManager.NETWORK_TYPE_GSM:
                                                                                      return " GSM";
                                                                                      case TelephonyManager.NETWORK_TYPE_CDMA:
                                                                                      case TelephonyManager.NETWORK_TYPE_1xRTT:
                                                                                      case TelephonyManager.NETWORK_TYPE_IDEN:
                                                                                      return " 2G";
                                                                                      case TelephonyManager.NETWORK_TYPE_GPRS:
                                                                                      return " GPRS (2.5G)";
                                                                                      case TelephonyManager.NETWORK_TYPE_EDGE:
                                                                                      return " EDGE (2.75G)";
                                                                                      case TelephonyManager.NETWORK_TYPE_UMTS:
                                                                                      case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                                                                      case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                                                                      case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                                                                      return " 3G";
                                                                                      case TelephonyManager.NETWORK_TYPE_HSPA:
                                                                                      case TelephonyManager.NETWORK_TYPE_HSDPA:
                                                                                      case TelephonyManager.NETWORK_TYPE_HSUPA:
                                                                                      return " H (3G+)";
                                                                                      case TelephonyManager.NETWORK_TYPE_EHRPD:
                                                                                      case TelephonyManager.NETWORK_TYPE_HSPAP:
                                                                                      case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                                                                                      return " H+ (3G++)";
                                                                                      case TelephonyManager.NETWORK_TYPE_LTE:
                                                                                      case TelephonyManager.NETWORK_TYPE_IWLAN:
                                                                                      return " 4G";
                                                                                      default:
                                                                                      return " 4G+";
                                                                                      }
                                                                                      }





                                                                                      share|improve this answer


























                                                                                        0












                                                                                        0








                                                                                        0







                                                                                        If you want to know which type of network it is, you can use this :



                                                                                        private String getNetworkClass() {
                                                                                        // network type
                                                                                        TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
                                                                                        int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
                                                                                        switch (networkType) {
                                                                                        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                                                                                        return "Unknown network";
                                                                                        case TelephonyManager.NETWORK_TYPE_GSM:
                                                                                        return " GSM";
                                                                                        case TelephonyManager.NETWORK_TYPE_CDMA:
                                                                                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                                                                                        case TelephonyManager.NETWORK_TYPE_IDEN:
                                                                                        return " 2G";
                                                                                        case TelephonyManager.NETWORK_TYPE_GPRS:
                                                                                        return " GPRS (2.5G)";
                                                                                        case TelephonyManager.NETWORK_TYPE_EDGE:
                                                                                        return " EDGE (2.75G)";
                                                                                        case TelephonyManager.NETWORK_TYPE_UMTS:
                                                                                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                                                                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                                                                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                                                                        return " 3G";
                                                                                        case TelephonyManager.NETWORK_TYPE_HSPA:
                                                                                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                                                                                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                                                                                        return " H (3G+)";
                                                                                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                                                                                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                                                                                        case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                                                                                        return " H+ (3G++)";
                                                                                        case TelephonyManager.NETWORK_TYPE_LTE:
                                                                                        case TelephonyManager.NETWORK_TYPE_IWLAN:
                                                                                        return " 4G";
                                                                                        default:
                                                                                        return " 4G+";
                                                                                        }
                                                                                        }





                                                                                        share|improve this answer













                                                                                        If you want to know which type of network it is, you can use this :



                                                                                        private String getNetworkClass() {
                                                                                        // network type
                                                                                        TelephonyManager mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
                                                                                        int networkType = mTelephonyManager != null ? mTelephonyManager.getNetworkType() : 0;
                                                                                        switch (networkType) {
                                                                                        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                                                                                        return "Unknown network";
                                                                                        case TelephonyManager.NETWORK_TYPE_GSM:
                                                                                        return " GSM";
                                                                                        case TelephonyManager.NETWORK_TYPE_CDMA:
                                                                                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                                                                                        case TelephonyManager.NETWORK_TYPE_IDEN:
                                                                                        return " 2G";
                                                                                        case TelephonyManager.NETWORK_TYPE_GPRS:
                                                                                        return " GPRS (2.5G)";
                                                                                        case TelephonyManager.NETWORK_TYPE_EDGE:
                                                                                        return " EDGE (2.75G)";
                                                                                        case TelephonyManager.NETWORK_TYPE_UMTS:
                                                                                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                                                                                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                                                                                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                                                                                        return " 3G";
                                                                                        case TelephonyManager.NETWORK_TYPE_HSPA:
                                                                                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                                                                                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                                                                                        return " H (3G+)";
                                                                                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                                                                                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                                                                                        case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                                                                                        return " H+ (3G++)";
                                                                                        case TelephonyManager.NETWORK_TYPE_LTE:
                                                                                        case TelephonyManager.NETWORK_TYPE_IWLAN:
                                                                                        return " 4G";
                                                                                        default:
                                                                                        return " 4G+";
                                                                                        }
                                                                                        }






                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Nov 21 '18 at 22:17









                                                                                        ChristianChristian

                                                                                        312216




                                                                                        312216






























                                                                                            draft saved

                                                                                            draft discarded




















































                                                                                            Thanks for contributing an answer to Stack Overflow!


                                                                                            • Please be sure to answer the question. Provide details and share your research!

                                                                                            But avoid



                                                                                            • Asking for help, clarification, or responding to other answers.

                                                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                                                            To learn more, see our tips on writing great answers.




                                                                                            draft saved


                                                                                            draft discarded














                                                                                            StackExchange.ready(
                                                                                            function () {
                                                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2919414%2fget-network-type%23new-answer', 'question_page');
                                                                                            }
                                                                                            );

                                                                                            Post as a guest















                                                                                            Required, but never shown





















































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown

































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown







                                                                                            Popular posts from this blog

                                                                                            Create new schema in PostgreSQL using DBeaver

                                                                                            Deepest pit of an array with Javascript: test on Codility

                                                                                            Costa Masnaga