GPS和网络提供商之间切换 [英] Switching between gps and network provider

查看:171
本文介绍了GPS和网络提供商之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我镶有细和粗精度都提供监听位置更新。据我所知,用细的位置将由GPS使用粗的位置会由网络提供商提供(从该链接的 http://www.alonsoruibal.com/using-two-locationproviders-on-android/ )。现在,如果GPS被禁用我想我的应用程序切换到网络,并在GPS启用切换回吧。这可以通过使用 onStatusChanged 正确的方法做?
我的问题是,这是code OK?

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
}

而且,如果更新是已经申请过,(以前在什么地方在我的code,我没有把这里)(这两个供应商),这将是确定的,如果我在这个onStatusChanged方法再次提出要求。或者我必须先删除更新?

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?

推荐答案

这看起来好像没什么问题。我建议添加了之前清除一切更新侦听。为了保持它的清洁和一贯的。

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

这篇关于GPS和网络提供商之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆