I'm reading a lot of conflicting information on this topic, so going to ask myself with some specific code examples. My Android app is getting "Network Unavailable" errors when trying to make an HTTP request from a background service, only when the phone is asleep. The phone is using the mobile network only when I get these errors (no wi-fi in the building).
I use this code to schedule my service:
static private void SchedulePoll(Context context,int minsFromNow)
{
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, minsFromNow);
Intent intent = new Intent(context, PSDroidBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
My service acquires a PARTIAL_WAKE_LOCK and then calls:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
At that point I'm getting a Network Not Available exception, only when phone in sleep mode.
Is it normal to have issues like this in sleep mode, or should I be able to make an Internet connection from my service? It seems like lots of apps check email and other things, so it should be normal and work most of the time?
I looked through the phones settings and made sure anything that was related to background connections was enabled. It is a Sprint HTC, Evo I think.