【问题标题】:Android network provider and gps provider handlingAndroid 网络提供商和 gps 提供商处理
【发布时间】:2014-03-27 06:46:23
【问题描述】:

我已经完成了这段代码,我要求更新位置。我想知道我是否正确实现了它。打开页面后,我请求更新(通过 gps 优先选择网络)...并在函数 onlocationchanged 中显示位置。我想问一下,当我做getlastknownlocation时,是否有可能在onlocationchanged中得到类似的位置空值? 我停止使用 ghetlastknownlocation,因为它有时会返回 nulll 并且值错误。

      boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean  isNetworkEnabled = lm
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (!isGPSEnabled && !isNetworkEnabled)
    {   
        ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
        progress.setVisibility(View.INVISIBLE);
        Utilities.showAlertDialog2(RegionalStoresMeActivity.this, "", ""+getResources().getString(R.string.nothing_enabled), false);
    }
    else if(isNetworkEnabled){
        // lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);       
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
    }
    else if(isGPSEnabled ) {                                                                                      
               lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,0, this);
    } 

【问题讨论】:

    标签: android gps-time


    【解决方案1】:

    仅当它们是具有适当纬度和经度的位置更新时才调用 onLocationChanged() 方法。这可能是最微小的可能性,但我从未经历过。

    你做得很好,但我想提出一个建议。 考虑一个场景-

    网络提供商已启用,您已请求位置更新。但由于某种原因,它不返回位置更新(在很多情况下,位置提供者不返回位置更新,也不是必须返回位置更新。)

    现在根据您的代码,它不会检查 GPS 提供商,因为它在“else if(isGPSEnabled)”中。所以我建议你只在下面的“if”语句中实现它

    if( isNetworkEnabled )
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
    
    if( isGPSEnabled )                                                                                      
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,0, this);
    

    【讨论】:

    • 好的,谢谢。不要改变位置我正在做 locationmanager.removeupdates
    • 如果你执行 locationmanager.removeupdates 那么它会停止监听位置更新。
    • 我只需要一次,当它打开页面时。
    • 那你就走吧。一旦您获得位置更新,您可以立即停止侦听进一步的位置更新
    猜你喜欢
    • 2012-02-22
    • 2014-05-04
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 2014-07-22
    相关资源
    最近更新 更多