views:

86

answers:

2

I looked for the answer but didn't find one - my question(s) follow:

I wrote code that works with the emulator (I send local coords vis DDMS).

The code also works on the device BUT, only after running Google Map. I'm guessing there's something missing in either the manifest or in the code but I don't know what it is and I could use some advice/suggestions.

Here are snippets of the relevant portions of the Code: (I wasn't able to figure out how to properly embed this stuff so it looked intelligent/organized but, I think you get the point...)

homeboy = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
locationListener = new MyLocationListener();
Location recentLoc = homeboy.getLastKnownLocation(LocationManager.GPS_PROVIDER);

I also have a LocationListener method (shown w/o it's four methods of checking for enabled, changed,...etc)
private class MyLocationListener implements LocationListener {
... etc.

I include this in the manifest file near the top:
<uses-permission 
android:name="android.permission.ACCESS_FINE_LOCATION" />

So, what's missing? Also, how to get location from wifi (as in getting the location by any means available short of saving the last location to a file and retrieving it).

Thanks for any input

+1  A: 

So, what's missing?

You are not doing anything to turn on GPS. The GPS radio is off most of the time, because it is a battery hog. Hence, getLastKnownLocation() returns null most of the time. Use requestLocationUpdates() or something to get Android to turn on GPS.

CommonsWare
A: 

I turn the GPS on via the Settings. Shouldn't that take care of turning it on? I turn it on before running my app (tried waiting a few minutes, too).

I seem not able to respond to your (any) answer without answering my own question....

headscratch
"I turn the GPS on via the Settings" -- no, you don't. You enable GPS via the settings. That just means apps have the ability to request the GPS radio give them fixes. "I seem not able to respond to your (any) answer without answering my own question" -- once you have more karma (that "8" below your name), you should be able to add comments.
CommonsWare
Thanks - I now have a clearer understanding and will see if I can take care of it... Thanks for the karma thing - I now see a button!
headscratch
Following up - I believe your answer is incorrect (at least partially). In the settings panel the "Enable" turns GPS x-mtr on/off. Also, this is where to turn on/off wifi x-mtr. To be able to use wifi (and mobile network), they are flagged to talk to apps via the Network panel in Setting. It was good try but, no cigar...
headscratch
@headscratch: "In the settings panel the "Enable" turns GPS x-mtr on/off." That is a side-effect of enabling and disabling GPS. Just because GPS is enabled does not mean that the radio is on and giving you fixes. Whether you like it or not, you are going to need to use `requestLocationUpdates()` or the like to cause the GPS radio to turn on and give you fixes. If the user has GPS disabled, you cannot get fixes no matter what you do, because the user has said she does not want to give you fixes.
CommonsWare
CommonsWare - thanks but, I'm not convinced. Here's a quote from HTC for my device "How do I turn the GPS on? Press Home > Menu and then tap Settings > Location. Select Use wireless networks and Enable GPS satellites."Also, the item for doing it say's "this will consume battery". If enabling it were only letting it talk to other apps, that wouldn't consume battery (by letting it communicate with them).And, as I mentioned, this action parallels with the emulator. Beside that, I am testing for updates and changes (as noted in the code I posted).Sure, I could still be missing your point.
headscratch
Problem SOLVED! I wasn't waiting long enough for connection to the settle in before calling to collect location data.Thanks for your help.
headscratch