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
Here the simple version:
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.
Modified case to return all three states and decide how to act on another piece of the application:
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: