【问题标题】:Android: LocationListener and getLastKnownLocationAndroid:LocationListener 和 getLastKnownLocation
【发布时间】:2014-05-21 19:28:23
【问题描述】:

我有一个每分钟都在运行的服务,并且应该不断跟踪位置。该服务实现了 LocationListener。我觉得我没有正确理解这一点,因为我实际上并没有使用 LocationListener 方法: onLocationChanged onProviderEnabled onProviderDisabled 等等

我的班级中有这些方法,但我实际上并没有对它们做任何事情。我所做的只是每次我的服务运行时,我都会根据可用的 GPS 和网络提供商调用 LocationManager.getLastKnownLocation。

这是我的代码的样子:

服务每分钟运行一次:

Intent i = new Intent(context, GPSTracker.class);
    PendingIntent pi = PendingIntent.getService(context, 0, i, 0);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if(isOn) {
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), POLL_INTERVAL, pi);
    } else {
        alarmManager.cancel(pi);
        pi.cancel();
    }

我处理 Intent:

protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "In onHandleIntent!");
    Location currentLocation = getLocation(this);
    if(currentLocation == null) {
        saveLocation(-8.0, -8.0, "nothing", "nothing", "nothing");
    } else {
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());
        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
            String city = addresses.get(0).getLocality();
            String country = addresses.get(0).getCountryCode();
            String state = addresses.get(0).getAdminArea();
            saveLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), city, state, country);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

getLocation() 才是真正的工作:

 public Location getLocation(Context context) {
    try {
        locationManager = (LocationManager) context
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

但似乎有些不对劲。一方面,就像我说的那样,我不使用任何 LocationListener 方法,而且它似乎也不是很准确。当我坐在电脑桌前用手机进行测试时,它每次都能找到它。然而,今天我有一次在工作时在我的手机上运行这个程序,还有一次当我走在街上时,这些地方的位置距离它们都只有一英里远。奇怪的是,这两个地方的位置完全相同。因为我没有打开 GPS,所以这两个都是网络提供商。

我做错了什么,如果我没有做错什么,我怎样才能更准确?

【问题讨论】:

  • 你从哪里得到这个代码???我认为这段代码是我写的:P:P
  • 为了您的信息,来自 GPS 的位置差异很大。所以你需要完全消除那些不适合你的点。
  • 我从这个博客得到代码:niravranpara.blogspot.se/2013/04/…
  • 是的,我知道它变化很大,但我认为我的主要问题是,在博客文章中,它不是在后台服务中运行,而是在应用程序旁边运行。即使应用程序不存在,我也需要我的工作。
  • 您可以轻松地将代码从 Activity 移动到服务。我没有确切的代码,但我有一个代码可以定期将用户的位置发送到服务器。如果您需要代码,请将您的电子邮件发送给我。

标签: android gps location locationlistener


【解决方案1】:

getLastKnownLocation () 将提供最后一个已知位置(不是当前位置)。如果设备已关闭并移动到另一个位置,则该位置可能已过期。

您需要使用 LocationClient 使用 Google Location Service 获取当前位置。集成起来很简单。您需要为 LocationClient 创建一个实例并连接。建立连接时有回调方法。连接后,您可以从 mLocationClient.getLastLocation() 中检索当前位置(返回当前可用的最佳最近位置)。

查看此链接https://developer.android.com/training/location/retrieve-current.html

【讨论】:

  • 所以我们必须使用 Google Play 服务才能获得当前位置?
  • 是的,因为 LocationClient 扩展了 GooglePlayServicesClient
  • 但是如果 Google Play 服务不可用怎么办?
  • 不行,需要确认 GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  • 我可以尝试使用 GooglePlayServices,但是如果 LocationListener 和 onLocationChanged 方法没有做我认为它会做的事情,那么它的目的是什么?
【解决方案2】:

由于您关心的是获得更准确的结果,因此您应该使用 GPS。您可以简单地添加一个 GPS 启用按钮来要求用户打开 GPS。

gpsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                isGpsON = true;
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);

        }
    });

您没有使用任何 LocationListener,但我认为您应该使用它来进行位置更新

@Override
public void onProviderEnabled(String provider) {
    getLocation(this);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 2013-12-24
    • 2012-05-28
    • 1970-01-01
    相关资源
    最近更新 更多