URLConnection when device is in sleep mode not working
<p>I am having problems with a background service while the device is in sleep mode. I have set an alarm that will start a service every morning at 9 AM. This service works fine while the device is awake, but fails when the device is asleep.</p>

<p>I've traced the issue to a URLConnection.openConnection() call shown below. The device cannot seem to open this connection while in sleep mode, it throws an IOException. <strike>I've used URL.openStream() to perform network calls when the device is asleep, and they work fine.</p>

<p>Can anyone explain why openStream() would work in sleep mode but openConnection() does not? Is there a way to get it working?</p>

<p>This works in sleep mode:</p>

<p>return new BufferedReader(new InputStreamReader(new URL(url).openStream()), 8192);</strike></p>

<p>This does not seem to work in sleep mode:</p>

<pre><code>URL actualURL = new URL(url.toString());
URLConnection c = actualURL.openConnection();
c.setConnectTimeout(10000);
c.setReadTimeout(20000);
InputStreamReader inputStreamReader = new InputStreamReader(c.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
</code></pre>

<p>EDIT: Fixed! I was making these calls in a background thread started by the service. When I took the functionality out of the background thread and into the service it started working in sleep mode. Strange behavior -> I was able to log all events in the thread but wasn't able to connect to the internet.</p>
