Reputation: 2363
My application needs to know when the Wifi or the Mobile data has been turned on or off by the user. Actually it shows when the network changes, if the device has connection or not, works fine. but I want to konw when the user is turning on/off the network manually.
Right know I have this on my Manifest.
<receiver android:name="pe.com.gps.broadcastreceivers.CheckForMobileDataBroadcastReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
Upvotes: 2
Views: 8609
Reputation: 77
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); if (!wifiManager.isWifiEnabled())Toast.makeText(this, "Wifi is not enabled", Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 5150
In onCreate method (or whenever you want to know that wifi is connected or not) use WifiManager . For example:
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
if (!wm.isWifiEnabled()) {
// wm.setWifiEnabled(true);
//Or do what you want in disable mode
} else {
// wm.setWifiEnabled(false);
//Or do what you want in enable mode
}
In this case ConnectivityManager also help you. Check this conversation.
Upvotes: 2
Reputation: 6380
When the network is changed, your CheckForMobileDataBroadcastReceiver will receive the broadcast message that the connection has changed. Once you receive that message, you can query the current network state. If you take a look at this article in the android documentation it details how you can access the active network.
ConnectivityManager has constants for Mobile/Wifi etc which can be used for comparing against the network to figure out its type.
Upvotes: 0