Saturday, December 8, 2012

Android: Incomplete location object, missing timestamp or accuracy - ERROR

 Recently, when using my own Mock GPS Provider, I was getting an exception on my Google Nexus phone:

Incomplete location object, missing timestamp or accuracy

this almost drove me mad, since I could not find any solution out in the net.

Also it seemd to happen only on Jelly Bean Android devices.

So I had to do what I usually do not like a lot... dive in the Android source code. Luckily it was an easy one.

The LocationManager does a check on the Location object (isComplete method). There we have the mess. A check is made for

if (mElapsedRealtimeNanos == 0)

but I am not able to set that variable anywere before JB.
So if you create a Location with an SDK prior to JB and then run the code on JB, it will throw you an exception.

There is a nice method that, as javadoc states, helps backwards compatibility:

  1. /**
  2.      * Helper to fill incomplete fields.
  3.      *
  4.      * Used to assist in backwards compatibility with
  5.      * Location objects received from applications.
  6.      *
  7.      * @see #isComplete
  8.      * @hide
  9.      */
  10.     public void makeComplete()

So what you need to do to make sure your mock LocationManager doesn't break, is simply check at runtime if the method exists through reflection:

Method locationJellyBeanFixMethod = Location.class.getMethod("makeComplete");
if (locationJellyBeanFixMethod != null) {

and if it does, invoke it on the "incomplete" location object.

Since it is anyway a Mock class, I have no problem with that solution. I just wonder if that is the way to go. I assume not, since in that case it would be documented somewhere.

But well, it works. Hope that helps someone out there.

Post a Comment