0

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.

eselk
  • 6,764
  • 7
  • 60
  • 93
  • Which device/model and mobile network is it? You say "Sprint HTV Evo" but can you include some more information in your OP, please. You can find the phone information underneath the battery after removing it...powered off. Also, you can use a System Info app to show this and OS/Android version information. And, yes, like you said in comments below...doing debugging is the only way to go. There are apps for that too, so you wont have to go through so much hassle getting that info (search _logcat_ in the market). Edit your original post with that info and you'll get more help. – TryTryAgain Jan 24 '12 at 03:42
  • Thank you very much, that logcat is awesome and exactly what I needed for future debugging. – eselk Jan 24 '12 at 16:12

2 Answers2

2

Turns out it was an application on the phone called "Juice Defender". I guess it is designed to save battery life, as soon as we disabled it, my app works. GMai also wasn't working with it enabled, so I feel pretty safe that there isn't much I could have done to design my app better... other than making the error message more intuitive.

eselk
  • 6,764
  • 7
  • 60
  • 93
-1

Even with a Partial_Wake_Lock you need something like:

Settings.System.putInt(getContentResolver(),
  Settings.System.WIFI_SLEEP_POLICY, 
  Settings.System.WIFI_SLEEP_POLICY_NEVER);

Taken from: How do I keep Wifi from disconnecting when phone is asleep?

Community
  • 1
  • 1
TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
  • That thread doesn't state this or not, so... does that also apply to the mobile network? It seems like it would be specific to wi-fi, and in my case the phone is using the mobile (G3) network. – eselk Jan 24 '12 at 02:20
  • Actually just found this which does say that is wi-fi only: http://developer.android.com/reference/android/provider/Settings.System.html#WIFI_SLEEP_POLICY – eselk Jan 24 '12 at 02:21
  • Oh, right, That is WiFi only, sorry. Do you have the proper permission in the manifest? `` I'd also point you to this: http://stackoverflow.com/questions/2039555/how-to-get-an-android-wakelock-to-work if you haven't already seen it. – TryTryAgain Jan 24 '12 at 02:35
  • Yes, I have that permission. I borrowed a couple of other devices for testing (my kids' :) and they are not having this same issue. Most be something unique to this one device, I'll have to do some more debugging -- wish that was easier in this mobile world :( – eselk Jan 24 '12 at 03:21
  • Ah. Good to know it works elsewhere...so it is in fact device specific, for the most part. I commented on your original post, there are somewhat easier ways to get some debug info...then update your post with that additional info. – TryTryAgain Jan 24 '12 at 03:45