Search code examples
androidandroid-locationandroid-alarmsandroid-wireless

Location request times out when alarm wakes device; do I need to reconnect wireless networks manually?


My app schedules an RTC_WAKEUP alarm that goes off at user-defined intervals. When it fires, I register with Location Services for coarse updates (city-level is more than good enough), wait up to 60 seconds for a single location report, unregister the listener, and then make a location-specific web query and do various things with the information that's returned.

I've been testing in environments where there should always be at least a wireless network available to use for a location fix. The procedure works fine almost all the time, but the log shows that once in a while, the app times out waiting for a location report. This seems to only happen when the alarm wakes the device from sleep and the device is deep inside a building where the mobile network connection is flaky. Under these circumstances the wireless network is (I think) the only possible source of location information, so my hunch is that either (a) the wireless network is not automatically reconnected when an alarm wakes the device from sleep (until someone tries to open a connection?), or (b) 60 seconds just isn't long enough.

Can anyone confirm whether either of these things is true? If it's (a), is there a command I can issue that amounts to "reconnect the wireless network and then get back to me?"

I could fiddle around with the timeout interval, but even if that appears to solve the problem, I won't be confident I've truly addressed the root cause until I understand what's going on...


Some nitty-gritty detail about the procedure: When the alarm is received, the app kicks off an IntentService that registers a LocationClient with Location Services. It then enters a wait() loop that gives up and returns failure after 60 seconds. The client in turn registers a LocationListener from its onConnected(). When that listener receives a report, it stores off the Location and notifyAll()s the waiting thread. This is what the LocationRequest looks like (I only want one report, but I want it as fast as possible):

LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_LOW_POWER);
request.setInterval(0);
request.setFastestInterval(0);
request.setNumUpdates(1);

Solution

  • The answer to the question in the title is: no, you don't need to manually reconnect the wireless network; the system will do this automatically (I suppose technically I can't promise this is true for all versions and all devices).

    So what was actually going on? Two things. First, although the device reconnects the wifi automatically, this is not an instantaneous process. I needed to wait for the ConnectivityManager to tell me I had a live internet connection before trying to grab stuff from the internet. Second, the device was not configured to get location information from wireless networks. Duh. Now that I've fixed those two things, the location request consistently succeeds.