0

I checked mobile network connection in my program and I followed the discussion in this link. My code is as follow.

        boolean internet_flag = true;
        ConnectivityManager conMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobile = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (!mobile .isAvailable())
        {
            internet_flag = false;
        }

        if(internet_flag == false)
        {
            Toast.makeText(this, "Internet connection is down, please check netwrok connection", Toast.LENGTH_LONG)
            .show();            

        }

I turned off Mobile data at my handphone and my wifi is connected to a WiFi-based SDcard reader device (so I am sure that I have no Internet connection), but I still have internet_flag == true. My questions are

(1)Why I still have internet_flag == true even though I turned it off.

(2)I would like to understand the internet connection in Android. According to this discussion when Wifi is on, 3G is automatically off (it is still true?). In the discussion, to get 3G connection back, Wifi is turned off again. In my case, I can't switch off Wifi as I have it connected to the SDcard reader, but I still want to force to 3G connection. How can I do that? Possible Thanks

Community
  • 1
  • 1
batuman
  • 7,066
  • 26
  • 107
  • 229

1 Answers1

1

try this methode (as context you can give the application's context or the activity one):

public static boolean isNetworkAvailable(Context context) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager
                    .getActiveNetworkInfo();
            return activeNetworkInfo != null
                    && activeNetworkInfo.isConnectedOrConnecting();
        }

to enable the wifi you can use this

public static void enableWifi(Context context) {
    WifiManager wifi = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    if (!wifi.isWifiEnabled()) {
        wifi.setWifiEnabled(true);
    }
}

you should add some permissions in your manifest:

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

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
ahmed_khan_89
  • 2,755
  • 26
  • 49
  • it is still the same. If I turned off both wifi and 3G. Yeah it is connectivity is false. But if my wifi has connection to a device (that device doesn't have internet connection), the return is still true. – batuman Feb 24 '14 at 15:06
  • try this in the first methode (change the return to this) : return connectivityManager.getActiveNetworkInfo().isConnected() && connectivityManager.getActiveNetworkInfo().isAvailable() && connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting(); //now you are checking seriously everything ... – ahmed_khan_89 Feb 24 '14 at 15:25
  • If I turned off both, it returns false. If I have Wifi access to the device (without inter connection), it comes back true. So can't check whether the device has internet access. – batuman Feb 24 '14 at 15:36
  • amazing !!! I hope you will find something :) because this one : connectivityManager.getActiveNetworkInfo().isConnected() should be true only if you have connection... good luck – ahmed_khan_89 Feb 24 '14 at 16:01
  • How I access the sdcard at the device through wifi is through tcp/ip protocol. Scanning all available devices attached and get the ip address of the device. Through that ip address based url, I access the sdcard and download the file. At that time, there is no internet access to the outside world. Is that scenario, connectivityManager considers the device has network connection? May be I should check at 3G connectivity only. Since wifi is connected, 3G is disconnected. – batuman Feb 25 '14 at 00:36
  • Looks this link has a solution to me. Will test tonight and update. stackoverflow.com/questions/2513713/how-to-use-3g-connection-in-android-application-instead-of-wi-fi – batuman Feb 25 '14 at 00:53