title: How can I get a WifiManager Object inside of a Service?
body: <p>I'm writing an app for Android that spawns a service that periodically checks the wifi state and changes it based on some conditions. My problem is that whenever I try to create a WifiManager object to check the state of the wifi (and perhaps control it), it gives me an uninitialized object. Here is the code:</p>
  
  <pre><code>public class WifiCheckerService extends Service {
  
      // Service code and run() method
  
      private void checkWifi() {
          WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
          System.out.println(wifi.getConnectionInfo());
          // Rest of code
      }
  }
  </code></pre>
  
  <p>And here is the line in the log when it gets to this code.</p>
  
  <pre><code>02-18 05:22:59.274: INFO/System.out(1170): SSID: &lt;none&gt;, BSSID: &lt;none&gt;, MAC: &lt;none&gt;, Supplicant state: UNINITIALIZED, RSSI: -200, Link speed: -1, Net ID: -1
  </code></pre>
  
  <p>Obviously I'm not getting the system WifiManager object. I know it has something to do with the fact that I am in a service which is a thread, but I am still having trouble understanding the concept and a little explanation about why it's not working and how I can fix it would be greatly appreciated.</p>
  
  <p>(On a sort of unrelated note, I think an explanation would also explain why I can't use the line <code>Toast.makeText(getBaseContext(), "Checking Wifi...", Toast.LENGTH_SHORT).show();</code> in the service)</p>