Search code examples
javaandroidnetwork-programmingwifi

Can we make an android app which works only when we connect to a specific Wi-fi Network?


I want to know whether we can make an android app which works only when we connect to a specific wi-fi Network? All help is much appreciated! I'm all out of ideas here. Thank you.


Solution

  • boolean onRightNetwork() {
        WifiManager wifiManager = (WifiManager) App.getContext()
                            .getSystemService(App.getContext().WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    
        return wifiInfo.getSSID().equals(NETWORK_NAME);
    }
    

    Run that in onResume, and just pop up a screen that says to connect to the right network if it returns false. You may also want to register a broadcast receiver to listen for wifi connect/disconnect events and perform this check as needed there as well.