An SO User
An SO User

Reputation: 24998

When downloading data over WiFi, how do I know I am connected using Wifi

Using ConnectivityManager, I can check if the user is connected to WiFi. If yes, my app begins downloading. After writing the data to local storage, I check again if the user is connected to WiFi. If not, I stop the downloading.

Now, what I want to know is.. how do I ensure that the HttpURLConnection made is via WiFi ?

Another feature is where the user is liberal about the downloads. user allows the app to download either via WiFi or via network.

Assume a scenario where the user has WiFi turned on. The app checks if WiFi is available and begins downloading. Later, user switches off WiFi or moves out of the signal range. What happens to the connection then? Does it switch over to using network?

Also, how do I ensure that the HttpURLConnection is made via network?

All in all, I just need to avoid a nasty situation where the user ends up spending a lot of money accidentally over download. This is not a very critical app, I am doing this just for practice

Upvotes: 0

Views: 678

Answers (1)

Hugo Hideki Yamashita
Hugo Hideki Yamashita

Reputation: 221

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

More information can be extracted from NetworkInfo. Just check this reference.

You'll need this permission:

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

Complementing: I am not sure if it's an Android default or a device configuration, but whenever there is an established wifi connection, 3G is automatically disabled. I tested in a few devices and all of them have this behavior.

Upvotes: 1

Related Questions