如果我的应用程序连接到没有互联网连接的wifi设备,我想关闭移动数据。
如果无法关闭移动数据,那么我想显示警告对话框,告诉用户关闭移动数据。 下面我正在使用的代码,帮我检查一下wifi连接的状态。
请帮帮我。
public boolean broadcastingInitialization() {
IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
connMgr = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
onWifi = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
Log.v("Mobile DATA STATE", String.valueOf(connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()));
switch (extraWifiState) {
case WifiManager.WIFI_STATE_DISABLED:
Log.d("Wifi:", "WIFI STATE DISABLED");
wifiState = "DISABLED";
wifiAlertInit(); //show the wifi alert dialog if wifi state is disabled
alert.show();
break;
case WifiManager.WIFI_STATE_ENABLED:
Log.d("Wifi:", "WIFI STATE ENABLED");
if (connMgr != null) {
mMobileData = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
Log.v("MOBILE DATA:", String.valueOf(mMobileData.getExtraInfo()));
}
if (mMobileData.getExtraInfo() != null) {
MobileDataAlertInit(); // show the Mobile data alert box if both wifi and mobile is on at same time
alert.show();
}
if (mMobileData.getExtraInfo() == null) {
alert.dismiss();
}
break;
}
}
};
if (connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getExtraInfo() == null && connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
getInitIPAddress(sActivity); // get IP address only when WIFI STATE is ENABLED
return true;
}
getApplication().registerReceiver(onWifi, filter);
return false;
}