0
public class InternetConnectionControl {
private NetworkInfo networkInfo;
private NetworkInfo getNetworkInfo() {
if (networkInfo == null) {
ConnectivityManager _cm = ServiceUtil.getConnectivityManager(MyApplication.getContext());
networkInfo = _cm.getActiveNetworkInfo();
}
return networkInfo;
}
public void setNetworkInfo(NetworkInfo networkInfo) {
this.networkInfo = networkInfo;
}
private boolean isConnectedToWifi() {
NetworkInfo info = getNetworkInfo();
return (info != null && info.isConnectedOrConnecting() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
private boolean isConnectedToMobile() {
NetworkInfo info = getNetworkInfo();
return (info != null && info.isConnectedOrConnecting() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
public boolean isConnectedToInternet() {
boolean isWifiConnected = isConnectedToWifi();
boolean isMobileConnected = isConnectedToMobile();
return isWifiConnected || isMobileConnected;
}
}
我可以檢查在我的應用程序中是否有Wi-Fi或移動數據可用,但是如何在Wi-Fi和移動數據都處於活動狀態時處理情況?在這種情況下,我如何強制使用移動網絡?當WiFi和手機可用時強制移動網絡連接