在 GPS 和网络提供商之间切换
在我的应用程序中,我将两个提供商设置为精细和粗略的精度来侦听位置更新。我了解,使用精细位置将由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说看起来不错。我建议在添加之前删除所有更新侦听器。保持干净和一致。
It looks fine to me. I would recommend removing any update listeners before adding anymore. To keep it clean and consistent.