开发者

How find Internet connection is available or not on Android 4G?

开发者 https://www.devze.com 2023-03-02 22:44 出处:网络
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 connecti

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);

    开发者_如何学JAVA    if(mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected() || telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED)
            return true;
        else
            return false;
    }

Thanks in Advance Nirav modh


use TYPE_WIMAX
but it's in API level 8

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


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.


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;
}


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;
    }


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消