2

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>   
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alejandro Cumpa
  • 2,118
  • 1
  • 24
  • 45
  • http://stackoverflow.com/questions/3262781/how-to-check-wifi-or-3g-network-is-available-on-android-device check here – Zohra Khan Jan 28 '14 at 15:12
  • possible duplicate of http://stackoverflow.com/questions/21226776/android-how-to-get-indicate-when-the-internet-connection-is-lost/21226848#21226848 – Shayan Pourvatan Jan 28 '14 at 15:13
  • Both threads are about connection lost, maybe I didnot explain properly. My app is already showing if the device has or not connection, but I want to know if the connection has been lost because of weak signal or because the user has disabled it. – Alejandro Cumpa Jan 28 '14 at 15:18
  • did you see my answer in that link? with that code you can detect network state change that i think you want. see that and if have any question about that tell me – Shayan Pourvatan Jan 28 '14 at 15:20
  • 1
    I saw it, and it does what I already did. It works fine and it detects when the mobile or wifi gets enabled or disabled. That job is done. The thing is that that method don't tell me if the connection has been lost because the user disable it or because the signal is too weak, it only tells if is connected or not. – Alejandro Cumpa Jan 28 '14 at 15:28
  • @El_Mochiq if you want that you tell me i think you need AlarmManager and in that you need check internet ( try to connect to your server or anything like this ) – Shayan Pourvatan Jan 28 '14 at 15:49
  • Searching a lot, I also think that I should do that, but I don't know what action should I handle. Do you know that? – Alejandro Cumpa Jan 28 '14 at 16:03
  • what is your mean about `I don't know what action should I handle`? – Shayan Pourvatan Jan 28 '14 at 16:07
  • I mean, I should have an AlarmManager for receiving the intent, but what should it be? this one? – Alejandro Cumpa Jan 28 '14 at 16:13
  • no, i think this not deference with my code, then you can't detect when signal is too weak ( i'm not sure that this code can't detect that ) but alarmManager is not good way too because for alarmManager we must use long interval (like 5 minute ) so if user connectivity lost in this interval this might be another problem, maybe service is a good way i'm not sure really. – Shayan Pourvatan Jan 28 '14 at 16:18

3 Answers3

2

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.

Community
  • 1
  • 1
Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • I already know if the wifi or the mobiledata is connected or not, but I want know if that is either enabled or disabled by the user or by the poor signal. – Alejandro Cumpa Jan 28 '14 at 15:11
  • isWifiEnabled() .. check the example – Ranjit Jan 28 '14 at 15:13
  • @RanjitPati OP must use broadcast Receiver to detect network state change, with your code OP just can detect network state in one moment – Shayan Pourvatan Jan 28 '14 at 15:17
  • @Shayanpourvatan you are right. But think once when you use a service for some server stuff and frequently start it through Alarmmanager followed by some pendingintent. At that time this detection is work for multiple times as you want. – Ranjit Jan 28 '14 at 15:23
  • I know what kind of connection the device get, either wifi or mobile, the thing is that I want to know when disabled, if the user is the one that disabled it or the weak signal make the device lost connection. – Alejandro Cumpa Jan 28 '14 at 15:25
  • @RanjitPati yes but if "start it through Alarmmanager followed by some pendingintent" – Shayan Pourvatan Jan 28 '14 at 15:25
  • Thanks, but the conversation in the link is about checking the status of the mobile service. I want to know if the current status is because of the user or because the weak signal. – Alejandro Cumpa Jan 28 '14 at 15:33
0

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.

jimmithy
  • 6,360
  • 2
  • 31
  • 29
0
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (!wifiManager.isWifiEnabled())Toast.makeText(this, "Wifi is not enabled", Toast.LENGTH_SHORT).show();
Shahan Mehbub
  • 69
  • 1
  • 4