简体   繁体   English

Android GPS位置侦听器

[英]Android GPS location listener

I am developing an location aware application that needs to know the users location. 我正在开发一个需要知道用户位置的位置感知应用程序。 The problem is that the application doesn't seem to work properly. 问题在于该应用程序似乎无法正常运行。

By properly I mean that every time I have to first open an application (like cardio trainer) in order to get a GPS signal and then run my program, otherwise the GPS icon on the notification bar is blinking like it blinks when the device tries to get GPS signal and after a while it stops and no location is returned! 正确地说,我的意思是每次我必须先打开一个应用程序(如有氧运动教练)才能获取GPS信号,然后运行我的程序时,否则通知栏上的GPS图标会闪烁,就像设备尝试执行以下操作时会闪烁一样获取GPS信号,过一会儿它就停止了,并且没有返回任何位置!

This is really weird because it shouldn't stop, is there an interval that android provides the GPS to get signal? 这真的很奇怪,因为它不应该停止,Android是否有间隔提供GPS来获取信号?

On the documentation I haven't found anything about that. 在文档中,我没有发现任何有关此的信息。

My code is as follows: 我的代码如下:

lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, locationListenerGps);
LocationListener locationListenerGps = new LocationListener() {
    public void onLocationChanged(Location location) {
        locationResult.gotLocation(location);
        Log.e("##################","MyLocation got GPS location accuracy:
        "+location.getAccuracy()+" Altitute: "+location.getAltitude()+" log:
        "+location.getLongitude()+" lat: "+location.getLatitude());
     }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
  };

That should open the GPS provider and register a listener called locationListenerGps that would be called by the location manager every 1 minute or 10 meters right? 那应该打开GPS提供程序并注册一个名为locationListenerGps的侦听器,位置管理器每1分钟或10米会调用一次,对吗?

why I have to open cardioTrainer first to get a GPS signal? 为什么我必须先打开cardioTrainer才能获得GPS信号?

Try placing the following debug code in your listener to see when your location provider is enabled or disabled. 尝试在您的侦听器中放置以下调试代码,以查看何时启用或禁用了位置提供程序。 This will tell you when your provider changes states and maybe shed some light on what is happening while your app is running. 这将告诉您提供商何时更改状态,并可能使您了解应用程序运行时正在发生的事情。

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    switch (status) {
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Toast.makeText(mCtx, "Your location is temporarily unavailable",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.OUT_OF_SERVICE:
        Toast.makeText(mCtx, "Your location is now unavailable", Toast.LENGTH_SHORT)
                .show();
        break;
    case LocationProvider.AVAILABLE:
        Toast.makeText(mCtx, "Your location is now available", Toast.LENGTH_SHORT)
                .show();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM