package org.pihole.dnsproxy; import android.provider.Settings; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.NetworkInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiInfo; import android.os.Bundle; import android.os.Handler; import android.util.Log; public class WifiListenerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); Intent dnsProxyService = new Intent(context, DNSProxyService.class); // start if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { // wait a few moments for wifi to be fully there (new Handler()).postDelayed(new Runnable() { @Override public void run() { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); // if (wifiInfo.getSSID() == setting.get(LOCAL_HOME_WIFI_SSID)) context.startService(dnsProxyService.setAction(DNSProxyService.ACTION_START)); } }, 1000); } // stop else if (networkInfo.getState().equals(NetworkInfo.State.DISCONNECTED) && DNSProxyService.isRunning()) { context.startService(dnsProxyService.setAction(DNSProxyService.ACTION_STOP)); } } }