在android

时间:2016-09-24 19:37:13

标签: android

一旦我的应用程序开始使用(网络和GPS)提供程序,我就开始收听位置更新。我的位置监听器接收第一个位置修复所需的时间通常太长,用户无法等待。直到我的位置听众提供更准确的位置, 所以我认为我应该通过调用getLastKnownLocation(String)来利用缓存的位置。

但实际上我有点困惑,我必须在我的代码中放置getLastKnownLocation(String)方法! 在requestLocationUpdates()之前或之后?在我的函数或onResume()??

请任何人帮助我,提前谢谢

这是我的功能: -

  private final LocationListener gpsLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
            locationManager.removeUpdates(networkLocationListener);
        Toast.makeText(activity,"New GPS location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
            case LocationProvider.AVAILABLE:
                Toast.makeText(activity,"GPS available again\n",Toast.LENGTH_SHORT).show();
                break;
            case LocationProvider.OUT_OF_SERVICE:
                Toast.makeText(activity,"GPS out of service\n",Toast.LENGTH_SHORT).show();
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                Toast.makeText(activity,"GPS temporarily unavailable\n", Toast.LENGTH_SHORT).show();
                break;
    }}

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(activity,"GPS Provider Enabled\n",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(activity,"GPS Provider Disabled\n",Toast.LENGTH_SHORT).show();
    }
};


private final LocationListener networkLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {

        Toast.makeText(activity,"New network location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
            case LocationProvider.AVAILABLE:
                Toast.makeText(activity,"Network location available again\n",Toast.LENGTH_SHORT).show();
                break;
            case LocationProvider.OUT_OF_SERVICE:
                Toast.makeText(activity,"Network location out of service\n",Toast.LENGTH_SHORT).show();
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                Toast.makeText(activity,"Network location temporarily unavailable\n",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(activity,"Network Provider Enabled\n",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(activity,"Network Provider Disabled\n",Toast.LENGTH_SHORT).show();
    }
};

这是onResume()函数中的代码: -

 locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000, 0, gpsLocationListener);
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 5000, 0,
            networkLocationListener);

1 个答案:

答案 0 :(得分:0)

应该在lastKnownLocation()之前调用{p> requestLocationUpdates(),但我注意到,一旦获取了某个位置,就会调用removeUpdates()。也许您想要使用requestSingleUpdate代替?

https://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(java.lang.String,android.app.PendingIntent)

应该注意的是,在this answer他们说你应该等待GPS稳定并避免使用requestSingleUpdate(),但如果你没有达到很高的准确度,那就更好了。