简体   繁体   English

使用OnStatusChanged从GPS_PROVIDER切换到NETWORK_PROVIDER?

[英]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.

相关问题 无法使用GPS_PROVIDER / NETWORK_PROVIDER获取用户位置 - Not getting user location using GPS_PROVIDER / NETWORK_PROVIDER getLastKnownLocation使用GPS_PROVIDER和NETWORK_PROVIDER返回NULL - getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER 来自Android中GPS_PROVIDER或NETWORK_PROVIDER的位置不一致 - Location from GPS_PROVIDER or NETWORK_PROVIDER in android is not consistent 如何从GPS_PROVIDER自动更改为NETWORK_PROVIDER - How to change automatically from GPS_PROVIDER to NETWORK_PROVIDER 如果位置管理器无法通过GPS_Provider获取位置,请切换到NETWORK_Provider - If Location Manager cannot fetch location with GPS_Provider, switch to NETWORK_Provider 如何提示用户启用GPS_PROVIDER和/或NETWORK_PROVIDER? - How to prompt user to enable GPS_PROVIDER and/or NETWORK_PROVIDER? 如何添加仅使用GPS_PROVIDER作为提供者而不使用NETWORK_PROVIDER触发的位置接近警报 - How to add a location proximity alert that triggers only using GPS_PROVIDER as provider and not using NETWORK_PROVIDER NETWORK_PROVIDER和GPS_PROVIDER在片段中返回null - NETWORK_PROVIDER and GPS_PROVIDER returns null in fragments 使用GPS_PROVIDER和NETWORK_PROVIDER获取当前位置的问题 - Issue in getting Current Location with GPS_PROVIDER and NETWORK_PROVIDER 在android中,GPS_PROVIDER有效,但NETWORK_PROVIDER不起作用 - in android, GPS_PROVIDER works, but NETWORK_PROVIDER does not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM