在网络和 GPS 提供商之间切换

发布于 2024-10-28 06:06:09 字数 250 浏览 6 评论 0原文

我想实现一个 locationListener,它将根据可用性在网络和 GPS 提供商之间切换。

例如,如果未启用 GPS,我希望它使用网络,但一旦 GPS 打开,我希望它停止侦听来自网络的位置更新并开始侦听 GPS。

同样,我希望它在 GPS 关闭后立即开始侦听来自网络的更新。

这可能吗?


子问题

GPS 提供位置修复的速度与网络一样快吗?


I want to implement a locationListener which will switch between network and GPS providers based on availability.

For example if GPS is not enabled I want it to use network but as soon as GPS is on then I want it to stop listening for location updates from network and start listening from GPS.

Similarly I want it to start listening for updates from network as soon as GPS is switched off.

Is that possible?


Subquestion

Is GPS as fast as network in providing a location fix?


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野却迷人 2024-11-04 06:06:09

当然,您只需获取网络和 GPS 的提供商并将您想要的任何内容传递给 locationManager.requestLocationUpdates()

当您想要停止侦听某个提供程序时,请使用您在 locationManager.requestLocationUpdates() 中指定的侦听器对象调用 locationManager.removeUpdates()

网络:

Criteria crit = new Criteria();
crit.setPowerRequirement(Criteria.POWER_LOW);
crit.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = locationManager.getBestProvider(crit, false);

GPS:

Criteria crit2 = new Criteria();
crit2.setAccuracy(Criteria.ACCURACY_FINE);
provider2 = locationManager.getBestProvider(crit2, false);

您可以使用 LocationManager.isProviderEnabled() doc< /a> 查看是否启用/禁用了相应的提供程序。 LocationManager 文档中提供了更多信息。

GPS 通常比网络慢得多,因为您必须找到 3 个以上的遥远卫星等。

Sure, you just get the providers for the network and GPS and pass whichever you want to locationManager.requestLocationUpdates().

When you want to stop listening to a certain provider, call locationManager.removeUpdates() with the listener object you specified in locationManager.requestLocationUpdates().

Network:

Criteria crit = new Criteria();
crit.setPowerRequirement(Criteria.POWER_LOW);
crit.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = locationManager.getBestProvider(crit, false);

GPS:

Criteria crit2 = new Criteria();
crit2.setAccuracy(Criteria.ACCURACY_FINE);
provider2 = locationManager.getBestProvider(crit2, false);

You can use LocationManager.isProviderEnabled() doc to see if the appropriate provider is enabled/disabled. There's more info available in the LocationManager docs.

GPS is usually much slower than network since you have to find 3+ far-away satellites, etc.

眼趣 2024-11-04 06:06:09

我正在用这个

LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    String locationProvider = LocationManager.NETWORK_PROVIDER;
    Location lastKnownLocation = locationManager
            .getLastKnownLocation(locationProvider);
    if (lastKnownLocation == null) {
        locationProvider = LocationManager.GPS_PROVIDER;
        lastKnownLocation = locationManager
                .getLastKnownLocation(locationProvider);
    }
    if (lastKnownLocation != null) {
        makeUseOfNewLocation(lastKnownLocation);
    }
    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            makeUseOfNewLocation(location);
        }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    // Register the listener with the Location Manager to receive location
    // updates
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    } else {
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }
}

I am using this

LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    String locationProvider = LocationManager.NETWORK_PROVIDER;
    Location lastKnownLocation = locationManager
            .getLastKnownLocation(locationProvider);
    if (lastKnownLocation == null) {
        locationProvider = LocationManager.GPS_PROVIDER;
        lastKnownLocation = locationManager
                .getLastKnownLocation(locationProvider);
    }
    if (lastKnownLocation != null) {
        makeUseOfNewLocation(lastKnownLocation);
    }
    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            makeUseOfNewLocation(location);
        }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    // Register the listener with the Location Manager to receive location
    // updates
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    } else {
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文