[英]Using OnStatusChanged to switch to NETWORK_PROVIDER from GPS_PROVIDER?
I'm working on an app that requires an accurate GPS fix. 我正在开发需要精确GPS定位的应用程序。 In the event that
GPS_PROVIDER
is enabled, but unavailable (like when you're in a tunnel), I want my app to switch to NETWORK_PROVIDER
. 如果
GPS_PROVIDER
已启用但不可用(例如在隧道中),我希望我的应用切换到NETWORK_PROVIDER
。
Can I use locationListener's OnStatusChanged OUT_OF_SERVICE
to identify if a GPS fix cannot be made? 我可以使用locationListener的
OnStatusChanged OUT_OF_SERVICE
来确定是否无法进行GPS修复吗?
Something like this: 像这样:
public void onStatusChanged(String provider, int status, Bundle extras) {
//0 = OUT_OF_SERVICE
if (status == 0) {
//try requesting updates using NETWORK_PROVIDER
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
//TOAST MESSAGE: GPS UNAVAILABLE ATTEMPTING FIX WITH NETWORK
...
Am I'm using OnStatusChanged
correctly? 我在正确使用
OnStatusChanged
吗? Is there a simple and better way of first attempting a GPS fix and if unavailable, switching to NETWORK? 是否有一种简单且更好的方法来首先尝试GPS修复,如果不可用,则切换到NETWORK?
Adding lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
添加
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
it will start fetching the location using network but once you come out from the Tunnel, it will start fetching from Network and GPS. 它将开始使用网络获取位置,但是一旦您从隧道中出来,它将开始从网络和GPS获取。 Using
location.getProvider()
you should be able to differentiate to know whether it's gps or network. 使用
location.getProvider()
您应该能够区分出它是gps还是网络。
If your GPS status isn't changing (eg, if you're always indoors without a GPS fix) while the app is running, some devices won't trigger the OnStatusChanged()
method. 如果在应用程序运行时GPS状态没有改变(例如,如果您始终在室内而没有GPS定位),则某些设备将不会触发
OnStatusChanged()
方法。
If you change GPS statuses while the app is running (eg, you're inside and can't get a fix and then walk outside and can get a fix, or vice versa), then the OnStatusChanged()
method should fire on all devices. 如果您在应用程序运行时更改GPS状态(例如,您在内部并且无法获得修复,然后在
OnStatusChanged()
走走可以得到修复,反之亦然),则应在所有设备上触发OnStatusChanged()
方法。
If you want a fully working open-source app to use as an example, try GPSTest by Mike Lockwood from the Android team. 如果您希望使用功能全面的开源应用作为示例,请尝试使用Android团队的Mike Lockwood的GPSTest。
GPSTest on Google Play Google Play上的GPSTest
Source code for GPSTest GPSTest的源代码
For more detailed information about GPS that is constantly updated even if your device can't get a fix, you might want to register a GPSStatusListener. 有关即使设备无法修复也不断更新的GPS的更多详细信息,您可能需要注册GPSStatusListener。
In your Activity, declare class variables: 在您的Activity中,声明类变量:
private LocationManager mService;
private GpsStatus mStatus;
...and add the method to handle the GPSStatus changes: ...并添加处理GPSStatus更改的方法:
public void onGpsStatusChanged(int event) {
mStatus = mService.getGpsStatus(mStatus);
switch (event) {
case GpsStatus.GPS_EVENT_STARTED:
// Do Something with mStatus info
break;
case GpsStatus.GPS_EVENT_STOPPED:
// Do Something with mStatus info
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
// Do Something with mStatus info
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
// Do Something with mStatus info
break;
}
}
Then in OnCreate() of your Activity to register the GPSStatusListener: 然后在您的Activity的OnCreate()中注册GPSStatusListener:
mService = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mService.addGpsStatusListener(this);
In the GPSTest app, the list of currently available satellites is shown on the screen with each GPSStatusListener update, based on this code . 在GPSTest应用程序中,基于此代码 ,每次GPSStatusListener更新都会在屏幕上显示当前可用卫星的列表。
This way, you'll receive active updates on the GPS status of system even if your phone can't get a GPS fix (and therefore may not trigger OnStatusChanged
of the LocationListener
). 这样,即使您的手机无法获得GPS定位(因此也可能不会触发
LocationListener
OnStatusChanged
),您也会收到有关系统GPS状态的活动更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.