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和移动数据都处于活动状态时,我该如何处理?在这种情况下,如何强制使用移动网络?
答案 0 :(得分:0)
您无法在Android 4.4上方开启/关闭移动数据。此功能因安全原因而停止,您可以使用对话框请求用户启用移动数据,然后如果启用则可以执行任务。