How to use integers to display a status


Ask by : WHDeveloper November 03, 2012 21:02

I found something interesting in the android OS, that I want to use in my own apps but how?

Like in the ConnectivityManager http://developer.android.com/reference/android/net/ConnectivityManager.html, you see status codes like

public static int TYPE_WIFI = 1;

When you call the connectivity manager with this code:

ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Connectivity_Service);
NetworkInfo iswifi = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (iswifi.isConnected()){ msg = "You got WIFI!"}

but if I'm having a class that needs to say if I'm having a internet connection, how can i let it return a status code like that?

Already thanks for your answers!:)

Wout

View original question

Answer by : AerinxNovember 03, 2012 21:02

Here the simple version:

/*@return boolean return true if the application can access the internet*/
public static boolean haveInternet(Context context){
    NetworkInfo info = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (info==null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to disable internet while roaming, just return false
        return true;
    }
    return true;
}

Here you have it with preferences for wifi, you can specify if you want some actions to be limited by a preference set to only use wifi. For example you can check for small downloads using withRoamingPref = false; returning true for any internet connection. Then check for large downloads using withRoamingPref = true, in this case, it will check if the user has enabled "only wifi" to download them and returns true if using wifi or true if the user allows the mobile connection to download them, returning false if there is no internet connection or if you are on mobile connection and the user has disabled it to download heavy files.

public static boolean haveInternet(Context context, boolean withRoamingPref){
    NetworkInfo info = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (info==null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to disable internet while roaming, just return false
        if(withRoamingPref) {
            SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            boolean onlyWifi = settings.getBoolean("onlyWifi", false);

            if (onlyWifi) {
                return false;

            }else{
                return true;
            }

        }else{
            return true;
        }
    }
    return true;
}

Modified case to return all three states and decide how to act on another piece of the application:

/*@return int return 0 if the application can't access the internet
    return 1 if on mobile data or 2 if using wifi connection*/
public static int haveInternet(Context context){
    NetworkInfo info = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (info==null || !info.isConnected()) {
        return 0;
    }
    if (info.isRoaming()) {
        return 1;
    }else{
        return 2;
    }
}

PS: Instead of the numbers you should assign each state with a static int and then return that instead of the raw int. For example, declare at the start of the class:

public static int INTERNET_DISABLED = 0;

then instead of "return 0;" you'll use "return INTERNET_DISABLED;"

this way you don't have to remember what means each number, you'll check in other segment of the app against the names as in:

if(myClass.haveInternet == myClass.INTERNET_DISABLED){
     //TODO show no internet message
}
View original answer