【问题标题】:LocationManager.requestLocationUpdates add or update?LocationManager.requestLocationUpdates 添加或更新?
【发布时间】:2013-06-05 05:46:13
【问题描述】:

我正在查看 LocationManager 以获得定期报告位置的最佳位置。我对 LocationManager.requestLocationUpdates 方法很好奇。如果我用不同的 minTime 和 minDistance 多次调用此方法而不调用 removeUpdates 会发生什么? 它会添加新请求还是仅更新现有请求?
我正在尝试对此进行测试,目前并不容易。您的回答将不胜感激。

【问题讨论】:

  • 谢谢大家回答我。

标签: android locationmanager


【解决方案1】:

它将更新现有的请求。

【讨论】:

  • 相当老的问题,但我今天读到了 :) 我们在这里错过了一些来源!
  • 来源确实很好。不过我可以确认这个答案是正确的。
【解决方案2】:

您可以多次注册。 请参阅下面的代码。

package com.test.locationmanager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationManagerStatus extends Activity {

private LocationManager locationManager;
private TextView textView;
private final LocationListener gpsLocationListener =new LocationListener(){

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "GPS available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "GPS out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt + "GPS temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        locationManager.removeUpdates(networkLocationListener);
        textView.setText(textView.getText().toString()
                + "New GPS location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};
private final LocationListener networkLocationListener =
                                                    new LocationListener(){

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras){
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "Network location available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "Network location out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt
                    + "Network location temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        textView.setText(textView.getText().toString()
                + "New network location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.textview);
    locationManager = (LocationManager) 
            getSystemService(Context.LOCATION_SERVICE);
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 5000, 0,
            networkLocationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            3000, 0, gpsLocationListener);
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(networkLocationListener);
    locationManager.removeUpdates(gpsLocationListener);
}
}

【讨论】:

  • 这段代码有效吗?你能用这段代码得到经纬度吗?
  • 您在设备中进行过测试吗?我问的原因是我一段时间以来一直在尝试寻找 LocationManager.GPS_PROVIDER 的工作示例。在这里查看我的问题stackoverflow.com/questions/15627956/…
  • 是的..这是来自一个现场项目。
  • 我试过你的代码。只有网络位置有效。 Gps 没有来。 @blganesh101
  • 它对我有用.. 你在哪部手机上试用它?打开/关闭 gps,您可以看到正在调用 GPS 提供程序回调。当位置锁定时,您将获得 gps 更新..
【解决方案3】:

为 android 使用新的 Location API's,他们提高了它的准确性并降低了电池效率。

【讨论】:

    猜你喜欢
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多