user3278077
user3278077

Reputation:

How to get notified when the users Wifi changes from one network to other

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.

Upvotes: 1

Views: 906

Answers (3)

Jitesh Upadhyay
Jitesh Upadhyay

Reputation: 5260

  • 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

Upvotes: 1

Lucifer
Lucifer

Reputation: 29642

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>
    

Upvotes: 1

Robin
Robin

Reputation: 10368

  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

Upvotes: 0

Related Questions