kostas
kostas

Reputation: 55

How to check if wifi/3g is enabled?

I have a button that presents some RSS news. My problem is that I would like to present a toast message "there is no internet connection" if the wifi is disabled or there is no 3g connection. I have to make it work if the wifi is closed to pop out the message but my problem is:

I want the application to check if the user has wifi enabled or 3g connection. If there is 3g to go the new activity, if there is wifi to check not only if it is enabled but if the user is connected to a network and then go to the new activity. If the 3g or the wifi is disabled, I just want to present the message I wrote before.

This is the code I have written as now but I don't really know how to continue...

The manifest:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

And the source for my onCreate method:

@Override 
public void onCreate(Bundle icicle) 
{ 
        super.onCreate(icicle);
        setContentView(R.layout.main2);

        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        if (wifiManager.isWifiEnabled()) 
        {

            wifiManager.setWifiEnabled(true); // an einai anoixto asto anoixto
        } 
        else 
        {

            Toast.makeText(nea.this, "Χρειάζεστε σύνδεση στο internet", Toast.LENGTH_SHORT).show();
            finish();
        }

        loadFeed(ParserType.DOM);
    }

Upvotes: 0

Views: 9150

Answers (5)

Kvant
Kvant

Reputation: 91

ConnectivityManager conMan = ((ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE));
boolean isWifiEnabled = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isAvailable();
boolean is3GEnabled = !(conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED
                        && conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getReason().equals("dataDisabled"));

Upvotes: 9

hector6872
hector6872

Reputation: 1336

private static boolean isConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = null;
        if (connectivityManager != null) {

            networkInfo = connectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (!networkInfo.isAvailable()) {
                networkInfo = connectivityManager
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            }
        }
        return networkInfo == null ? false : networkInfo.isConnected();
    }

Upvotes: 0

sandeep
sandeep

Reputation: 947

For checking whether 3g is enabled, you can use the below code also. but if the network doesnt support 3g, this would return false.

int type = telephonymanager.getNetworkType();

if (type == TelephonyManager.NETWORK_TYPE_HSDPA
    || type == TelephonyManager.NETWORK_TYPE_HSPA
    || type == TelephonyManager.NETWORK_TYPE_HSUPA
    || type == TelephonyManager.NETWORK_TYPE_UMTS
    || type == TelephonyManager.NETWORK_TYPE_EVDO_0
    || type == TelephonyManager.NETWORK_TYPE_EVDO_A) {

    Toast.makeText(RemoteService.this, "network type " + type, Toast.LENGTH_SHORT).show();
    threegmode = true;
}
else {
    threegmode = false;
}

Upvotes: 2

Aleadam
Aleadam

Reputation: 40401

It is more complicated that just detect whether 3g or wifi is enabled or not. Very low signals could prevent the app to fetch the feed even when "3g is enabled". Same with wifi (is is a public hotspot? Does it go through the firewall?

Ping your server and set a timeout. If you don't get a response in a set amount of time (let's say 1-5 seconds), then show the toast, or even better a button to modify wireless settings (http://developer.android.com/reference/android/provider/Settings.html#ACTION_WIRELESS_SETTINGS)

Upvotes: 3

Vicente Plata
Vicente Plata

Reputation: 3380

You should present that message if there is an IOException or similar according to the HTTP API you're using (HttpClient for example). This way you won't make unnecessary extra-calls to Wifi manager and related services.

If you can't fetch the RSS, then the user doesn't have a data service (either Wifi or 3G).

Upvotes: 2

Related Questions