简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM