2014-03-27 65 views
0

我已经完成了这个代码,我要求位置更新。我想知道我是否正确实施它。一旦打开页面,我要求更新(首选gps网络)......并且我在onlocationchanged函数中显示位置。我想问是否可以在onlocation改变我得到位置null类似,当我getlastknownknownlocation? 我停止使用ghetlastknownlocation,因为它有时会返回nulll和错误的值。Android网络提供商和GPS提供商处理

 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); 
    } 

回答

0

的onLocationChanged()方法时,他们的是与适当的纬度和经度的位置更新只调用。这可能是微乎其微的可能性,但我从来没有经历过。

我已经做好了,但我想提出一个建议。 考虑一个场景 -

网络提供商已启用,并且请求位置更新。但由于某种原因,它不返回位置更新(有很多情况下位置提供商不返回位置更新也不是强制返回位置更新。)

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

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

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

ok thx。不要onlocation改变我正在做locationmanager.removeupdates – user3278732

+0

如果你做locationmanager.removeupdates,那么它会停止监听位置更新。 – kevz

+0

我只需要一次,当它打开页面。 – user3278732