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

发布于 2024-11-28 21:13:37 字数 1429 浏览 2 评论 0原文

在我的应用程序中,我将两个提供商设置为精细和粗略的精度来侦听位置更新。我了解,使用精细位置将由 GPS 提供,使用粗略位置将由网络提供商提供(来自此链接 http://www.alonsoruibal.com/using-two-locationproviders-on-android/)。现在,如果 GPS 被禁用,我希望我的应用程序切换到网络,并且当 GPS 启用时切换回网络。这可以使用 onStatusChanged 方法来完成,对吗? 我的问题是,这段代码可以吗?

public static final int OUT_OF_SERVICE = 0;
public static final int TEMPORARILY_UNAVAILABLE = 1;
public static final int AVAILABLE = 2;

Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
providerFine = locationManager.getBestProvider(criteria, true);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
providerCoarse = locationManager.getBestProvider(criteria, true);

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    if (provider.equals(providerFine)) {
        if (status == 0) {
            locationManager.requestLocationUpdates(providerCoarse, 60000,20,this);
        }
        if (status == 2) {
            locationManager.requestLocationUpdates(providerFine, 60000, 20, this);
        }
    }// provider == fine
}

而且,如果已经请求更新(在我的代码中之前的某个地方,我没有将其放在这里)(对于两个提供程序),如果我在此 onStatusChanged 方法中再次请求它们可以吗?或者我必须先删除更新?

In my app I set both providers with fine and coarse accuracy to listen for location updates. I understand that using fine the location will be provided by the gps, and using coarse the location will be provided by the network provider (from this link http://www.alonsoruibal.com/using-two-locationproviders-on-android/). Now if the gps is disabled I want my app to switch to network, and when gps is enabled to switch back to it. This can be done using onStatusChanged method right?
My question is, is this code ok?

public static final int OUT_OF_SERVICE = 0;
public static final int TEMPORARILY_UNAVAILABLE = 1;
public static final int AVAILABLE = 2;

Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
providerFine = locationManager.getBestProvider(criteria, true);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
providerCoarse = locationManager.getBestProvider(criteria, true);

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    if (provider.equals(providerFine)) {
        if (status == 0) {
            locationManager.requestLocationUpdates(providerCoarse, 60000,20,this);
        }
        if (status == 2) {
            locationManager.requestLocationUpdates(providerFine, 60000, 20, this);
        }
    }// provider == fine
}

And also, if updates are already requested, (somewhere before in my code, I didn't put that here) (for both providers), would it be ok if I request them again in this onStatusChanged method. Or I have to remove updates first?

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

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

发布评论

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

评论(1

千仐 2024-12-05 21:13:37

对我来说看起来不错。我建议在添加之前删除所有更新侦听器。保持干净和一致。

It looks fine to me. I would recommend removing any update listeners before adding anymore. To keep it clean and consistent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文