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:
- /**
- * Helper to fill incomplete fields.
- *
- * Used to assist in backwards compatibility with
- * Location objects received from applications.
- *
- * @see #isComplete
- * @hide
- */
- 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) {
locationJellyBeanFixMethod.invoke(location);
}
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.
2 comments:
Thanks so much for posting this. Guaranteed theres going to be a few people scratching their heads at that error message.
Thank you, it works.
Post a Comment