Issue
I try to prevent automatic connection to WiFi and displaying dialog box after turning on WiFi adapter from notification bar.
I register programmatically BroadcastReceiver that receives WIFI_STATE_CHANGED_ACTION. In WIFI_STATE_ENABLED I remove network from the configured network list. But it does not work.
I register BroadcastReceiver in:
@Override
protected void onStart() {
super.onStart();
registerReceiver(wifiStatusReceiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
}
And unregister in onStop method.
BroadcastReceiver code:
public class WiFiStatusReceiver extends BroadcastReceiver {
private WifiManager wifiManager;
@Override
public void onReceive(Context context, Intent intent) {
wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
if(!isInitialStickyBroadcast()) { //it is not a sticky intent
switch(wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_ENABLING:
Toast.makeText(context, R.string.enabling_wifi, Toast.LENGTH_SHORT).show();
break;
case WifiManager.WIFI_STATE_ENABLED:
Toast.makeText(context, R.string.enabled_wifi, Toast.LENGTH_SHORT).show();
wifiManager.disconnect();
if(!wifiManager.getConfiguredNetworks().isEmpty()) { //there are some networks
for(WifiConfiguration wifiConfig :wifiManager.getConfiguredNetworks()) {
wifiManager.removeNetwork(wifiConfig.networkId);
}
}
break;
case WifiManager.WIFI_STATE_DISABLING:
Toast.makeText(context, R.string.disabling_wifi, Toast.LENGTH_SHORT).show();
break;
case WifiManager.WIFI_STATE_DISABLED:
Toast.makeText(context, R.string.disabled_wifi, Toast.LENGTH_SHORT).show();
break;
}
} else { //is a sticky intent
wifiManager.disconnect();
if(!wifiManager.getConfiguredNetworks().isEmpty()) { //there are some networks
for(WifiConfiguration wifiConfig :wifiManager.getConfiguredNetworks()) {
wifiManager.removeNetwork(wifiConfig.networkId);
}
}
}
}
}
Any suggestions ?
Solution
Try this to Disable the Wifi............. and when needed enable it.........
boolean isOn = false;
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(isOn);
Answered By - Kumar Vivek Mitra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.