2

I have used Following Code For Wheather Intenet conection is available or not, its works fine upto 3G systems. but Its not working for 4G technology. Anybody have idea about How find Internet connection is Avlable or not on 4g? if any body have democode for it, please provide it

Code:

public static boolean checkConnection(Context c)
{
        ConnectivityManager mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager telephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);

        if(mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected() || telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED)
            return true;
        else
            return false;
    }

Thanks in Advance Nirav modh

Pepe
  • 6,360
  • 5
  • 27
  • 29
Nirav Modh
  • 757
  • 1
  • 8
  • 18

5 Answers5

1

use TYPE_WIMAX
but it's in API level 8

http://developer.android.com/reference/android/net/ConnectivityManager.html#TYPE_WIMAX

Joe Ho
  • 918
  • 3
  • 13
  • 28
  • My dear friends, How to check LTE(4G) connection Available or not? Here 4G Network is not available. How to test it you have any idea about it? – Nirav Modh May 03 '11 at 06:06
  • if no 4G/LTE network in your environment, you maybe need some equipments. Do you have 4G/LTE devices ? If no, why you want to have this kind request/problem? Or you have to simulate the 4g/LTE environment, but if you have no 4G/LTE device and network, what is this for ? – Joe Ho May 03 '11 at 07:56
  • I have a VZW LTE device (HTC Thunderbolt), and I just successfully tested that it's working with just TYPE_MOBILE. – Artem Russakovskii Sep 28 '11 at 20:31
0

Best way to check all internet connections is this short and to the point code:

public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] allNetworkInfo = connectivity.getAllNetworkInfo();
        if (allNetworkInfo != null)
            for (NetworkInfo info : allNetworkInfo)
                if (info.isConnectedOrConnecting()) {
                    return true;
                }
    }
    return false;
}
0

Try this:

public static Boolean check_connection(final Context _context)
    {
        boolean connected;

        ConnectivityManager conectivtyManager = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (conectivtyManager.getActiveNetworkInfo() != null
                && conectivtyManager.getActiveNetworkInfo().isAvailable()
                && conectivtyManager.getActiveNetworkInfo().isConnected())
        {
            connected = true;

        } else
        {
            connected = false;
        }
        return connected;
    }
Ludger
  • 362
  • 3
  • 16
0
public static boolean checkConnection(Context c)
{
    ConnectivityManager connectivityManager = (ConnectivityManager)
        getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo!=null&&activeNetInfo.isConnected()){
        Log.e(TAG, "Net connected");
        return true;
    }else{
        Log.e(TAG, "Net Disconnected" );
        return false;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
DevChamp
  • 188
  • 2
  • 2
  • 14
0
Context context = MainActivity.this;
    public synchronized static boolean isNetAvailable(Context context){

            boolean isNetAvailable=false;

            if ( context != null ){
                ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                if ( mgr != null ){
                    boolean mobileNetwork = false;
                    boolean wifiNetwork = false;
                    boolean wiMaxNetwork = false;

                    boolean mobileNetworkConnecetd = false;
                    boolean wifiNetworkConnecetd = false;
                    boolean wiMaxNetworkConnected = false;

                    NetworkInfo mobileInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                    NetworkInfo wifiInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    NetworkInfo wiMaxInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);

                    if ( mobileInfo != null )
                        mobileNetwork = mobileInfo.isAvailable();                   

                    if ( wifiInfo != null )
                        wifiNetwork = wifiInfo.isAvailable();

                    if(wiMaxInfo != null)
                        wiMaxNetwork = wiMaxInfo.isAvailable();

                    if(wifiNetwork == true || mobileNetwork == true || wiMaxNetwork == true){
                        mobileNetworkConnecetd = mobileInfo.isConnectedOrConnecting();
                        wifiNetworkConnecetd = wifiInfo.isConnectedOrConnecting();
                        wiMaxNetworkConnected = wiMaxInfo.isConnectedOrConnecting();
                    }
                    isNetAvailable = ( mobileNetworkConnecetd || wifiNetworkConnecetd || wiMaxNetworkConnected );
                }
            }
            return isNetAvailable;
        }

Check for value of isNetAvailable is true in all the 3 cases this works for me, wish work for u too

Note : 4G availability will be introduces API level 8 onwards.

Richa
  • 3,165
  • 1
  • 22
  • 26