1

I have an app in which I need to detect users Wifi connection and based on that, the user can see some data. The point is that I want to get notified when the user was on some other network while starting the app and moves to some other network while the app was running. Let me make a scenario:

Suppose I have 2 wifi, one inside my house and other outside. When starting the app, I was inside the house and accordingly the data shown to me was "ABC". Now when I move outside the house, my app should give a notification and kill the activity and hence I should not be able to see the same data "ABC" when I am outside the house.

flx
  • 14,146
  • 11
  • 55
  • 70
  • possible duplicate of [How to be notified on wifi network status change?](http://stackoverflow.com/questions/3119607/how-to-be-notified-on-wifi-network-status-change) – flx Feb 26 '14 at 04:53
  • hi akshat a code snippet is at answer please have a look, hope it will help you!! – Jitesh Upadhyay Feb 26 '14 at 05:16

3 Answers3

1

Each time the user changes the connection you can catch in BroadcastReceiver.

  • First of all you require to declare following permissions in AndroidManifest.xml

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    
  • Create a BroadcastReceiver

    public class BrodcastNetwork extends BroadcastReceiver 
    {
          @Override
          public void onReceive(Context context, Intent intent) 
          {
                  // Write your code here
          }
    }
    
  • Apply the filters to BroadcastReceiver

      <receiver android:name="com.example.datausage.BrodcastNetwork" >
          <intent-filter>
              <action android:name="android.net.conn.CONNECTIVITY_CHANGE" >
              </action>
          </intent-filter>
      </receiver>
    
Lucifer
  • 29,392
  • 25
  • 90
  • 143
1
  • Write a BroadcastReceiver as follows

    public class TheBroadcastReceiver extends BroadcastReceiver 
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            WifiManager wifiManager = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            NetworkInfo datainfo = intent
                    .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (datainfo != null) 
            {
                if (datainfo.getType() == ConnectivityManager.TYPE_WIFI) 
                {
                    //have different network states here
                    if (datainfo.getState() == datainfo.State.CONNECTING || datainfo.getState() == datainfo.State.CONNECTED) {
                        //work accordingly
                    }
                }
            }
        }
    }
    
  • Register a BroadcastReceiver and register these entries at manifest

        <receiver android:name="yours package details like com.a.b.c.TheBroadcastReceiver " >
              <intent-filter>
                   <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
                   <action android:name="android.net.wifi.STATE_CHANGE" />
               </intent-filter>
       </receiver>
    
  • Add following permission sets at manifest

      <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
      <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    
  • Also visit http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
  • Hi Jitesh, thanx for the help but can you tell me where I will be notified that the Wifi has been changed. I mean suppose I am on wifi named "Inside", and I move to "Outside", can you please tell me where to write the code to toast a message like "Wifi has been changed from "Inside" to "Outside"." –  Feb 26 '14 at 05:20
  • hi i wrote a comment on given code as //work accordingly..there you need to work accordingly – Jitesh Upadhyay Feb 26 '14 at 05:21
  • And what is the section in which there is "//have different network states here".? What network states does it mean.? –  Feb 26 '14 at 05:30
  • as you can see there is a if checking after it and it is anside after checking for wifi state ConnectivityManager.TYPE_WIFI!! – Jitesh Upadhyay Feb 26 '14 at 05:31
  • basically you are getting different network states after that comment, please see the code – Jitesh Upadhyay Feb 26 '14 at 05:32
  • @Kedarnath thankyou for editing and pointing it correctly. i added you on g talk as you have given mail id at yours profile – Jitesh Upadhyay Feb 26 '14 at 07:46
  • @JiteshUpadhyay ok, will talk with you in evening time. – Lucifer Feb 26 '14 at 07:58
0
  1. Register a broadcast receiver to listen for the Action "android.net.ConnectivityManager.CONNECTIVITY_ACTION"
  2. When connection changed to "connected" state, call WifiManager.getConnectionInfo().getBSSID() to check the current access point ID
Robin
  • 10,052
  • 6
  • 31
  • 52