Android check WIFI status (disconnected or user changed WIFI) How to FLAG it? Android check WIFI status (disconnected or user changed WIFI) How to FLAG it? multithreading multithreading

Android check WIFI status (disconnected or user changed WIFI) How to FLAG it?


Check the network connected Name by using:

public String getWifiName(Context context) {    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);    if (manager.isWifiEnabled()) {         WifiInfo wifiInfo = manager.getConnectionInfo();         if (wifiInfo != null) {            DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());            if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) {                return wifiInfo.getSSID();            }        }    }    return null;}

if this name matches your networkSSID, i.e. XYZ, then resume the service, else if it doesn't match, then stop the service:

if getWifiName(this).compareTo("XYZ") == 0 {  //XYZ is your network name on which you want to resume the service    //code to resume} else {    //code to stop the service}


This is how I handle it in my app:

public class WifiStateWatcher extends BroadcastReceiver {    private MainActivity activity;    public WifiStateWatcher(MainActivity activity) {        IntentFilter intentFilter = new IntentFilter();        intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);    }    @Override    public void onReceive(Context context, Intent intent) {        SupplicantState supState;        WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);        WifiInfo wifiInfo = wifiManager.getConnectionInfo();        supState = wifiInfo.getSupplicantState();        if (supState.equals(SupplicantState.COMPLETED)) {            //we are connected to Wi-Fi network        } else {            //we lost Wi-Fi connectivity        }    }}

You will need android.permission.ACCESS_WIFI_STATE permission


What you have done is almost correct. you need to check the network ssd name with the user connected wifi name.If it matched then do your part.

WifiManager wifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE);if (wifiManager.isWifiEnabled()) {    WifiInfo networkInfo = wifiManager.getConnectionInfo();    if (networkInfo != null) {        DetailedState state = WifiInfo.getDetailedStateOf(networkInfo .getSupplicantState());        if (state == DetailedState.CONNECTED ) {            return networkInfo.getSSID();        }     } } return null; 

Now you have the network SSID so try to check with the your wifi name and SSID then you will get to know the connection status.....Happy Programming