Issue
I want to download a file using cellular data. It seems that there is no API to specify the network connection when downloading a file, so I am trying to disable Wi-Fi before download.
It seems that I can use the setWifiEnabled
method, but the documentation does not mention whether it is blocking or not. I am assuming that it is not. Then, I must wait until the Wi-Fi is disabled before continuing to the next line.
val wifiManager = this.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.isWifiEnabled = false
(how to wait here until the system finishes disabling Wi-Fi?)
downloadFile()
What is the easiest way for this?
Solution
You can register a BroadcastReceiver to be notified whan a WiFi connection cahnged or try:
private boolean isConnectedViaWifi() {
ConnectivityManager connectivityManager = (ConnectivityManager) appObj.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return mWifi.isConnected();
}
with:
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if(isConnectedViaWifi()) //download
}}, 0, 1000);
Answered By - Amirhosein Heydari
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.