开发者

LocationListener works on emulator, not on phone

I'm having trouble getting a LocationListener to call the onLocationChanged() callback on my phone. When I run my code in the emulator, it works fine, the callback is called each time I do a geo fix.

When I run the application on my phone, nothing at all happens. The callback is never called. I have location enabled by both GPS and by Wireless in my settings. The application has all of the uses-permissions for location permissions.

Also, when I call getLastKnownLocation() on a LocationManager object, my application crashes. (Still, only on my stupid phone). Even if I try to catch an exception that's causing it to crash, it still just crashes, so I can't even get any information on what is causing it to crash. This is extremely frustrating.

LocationManager.getBestProvider() is returning GPS, and when I open google maps it finds my location in no time at all. What the heck is going on here? Is there some way I can figure out why it's crashing on my phone?

    private void setupLocListener(){    
    Criteria c = new Criteria();
    c.setAccuracy(Criteria.ACCURACY_FINE);
    c.setAltitudeRequired(false);
    c.setBearingRequired(false);
    c.setSpeedRequired(false);
    c.setCostAllowed(false);
    lm.requestLocationUpdates(lm.getBestProvider(c,true), 0, 0, new LocationListener() {
        @Override
        public void onLocationChanged(Location arg0) {
            map.setLocation(arg0);
        }
        public void onProviderDisabled(String arg0) {   
        }
        pu开发者_运维知识库blic void onProviderEnabled(String arg0) {    
        }
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {   }
    });
}


onLocationChanged() wont fire until you actually start receiving GPS coordinates.

By that I mean the chip has to warm up for about a minute or so from my experience before you start receiving data from it.

I usually start some other application and wait for it to prove that the GPS chip has warmed up before I go testing any of my GPS apps.

I know that you mentioned that it works properly in Google Maps but have you tried clearing your memory and restarting your application straight away afterwards?

Also getLastKnownLocation() is always null until you start receiving coords.


The Location framework pushes coordinates to your callback, when they become available. Depending on weather, etc. you may not get a "fix" initially. You should see the "GPS" indicator on the status bar when your listener is successfully registered. getLastKnownPosition() works just fine (it may return null); and Google Maps uses that, while it is waiting for an initial fix from the location provider. You may also want to see what other providers are available, e.g. cell-tower data, and attempt to obtain data from those (i.e. LKP), either instead of, or until, your "preferred" provider starts pushing data. Also, don't assume any particular service exists, e.g. LocationManager (Context.getSystemService() can return null), or any suitable provider exists, (getBestProvider() can return null). Your code will fail as-is on the right device with the right settings. If the documentation says null you must check for it, or users will be uninstalling it because it FC's all over the place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜