Make sure http connection with wifi

I have a small in-app service that collects data throughout the day and stores the data on an SD card. The premise is that the data will be large, so the app should not, under any circumstances, download this data on the user's plan.

There are situations where wifi exists but is unusable and the phone routes network requests as much as possible, using a data connection if possible.

Is there a way, however messy it may be, to ensure that the request will ONLY use the Wi-Fi connection, or FAIL otherwise?

EDIT:

While reading my question, I found that I didn't explain in detail what was going on. I am collecting data from the environment (some form of logging) and the data is accumulating on the SD card. When the user is near or at home (or anywhere else with WiFi support), the software should detect this and delete the contents of the SD card via HTTP POST requests. Files are 1MB or larger, so even if I detect the presence of Wi-Fi, it might change during transfer. Or worse, Wi-Fi might be on but not routed, so the phone routes the connection through your data plan and ruins your budget for a month.

+3


source to share


1 answer


You can achieve this in two ways:

1- Before you download, chech that the cellphone is currently connected via wifi, here is an example function



 public static boolean isConnectedToWiFi(Context context) {
    NetworkInfo activeNetwork = getConnectivityManager(context)
            .getActiveNetworkInfo();
    if (activeNetwork != null) {
        return activeNetwork.isConnected()
                && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
    }
    return false;
}

      

2. Listen for wide range link change android.net.conn.CONNECTIVITY_CHANGE

to monitor when link change, for example. transition to 3G

0


source







All Articles