2012-08-31 148 views
3

我想聆聽來自同一個偵聽和實施Android的位置監聽器

GPS和網絡位置提供商這是正確的做這件事:

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,metersToUpdate,this); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,metersToUpdate,this); 

將它的用戶需要爲提供相同的方法?

+1

如果你之前先閱讀該文件將是巨大的在這裏問。 http://developer.android.com/guide/topics/location/strategies.html – rajpara

+0

是的,你可以,但你需要管理提供者的準確性,當然你要怎麼做纔是你的。同時設置提供程序以儘可能快地發送更新將消耗大量的功率,因此您可能需要考慮是否真的需要這些功能。 – Idistic

+0

另請查看[本教程出處](http://www.anddev.org/novice-tutorials-f8/gps-and-network-location-t16563.html) – jnthnjns

回答

1

谷歌表示here

您還可以請求位置更新從GPS,並通過調用requestLocationUpdates(網絡位置提供商兩者)兩次,一次用於NETWORK_PROVIDER和一次GPS_PROVIDER。

+1

的行很好的答案,的確非常有幫助。 – rajpara

+0

是不是我問過的? –

+0

你問:可以使用它嗎?它是否會爲這兩個提供者使用相同的方法? ||谷歌已回答:是的,你可以做到這一點,他們將爲兩個提供商做相同的功能! –

1

很簡單1.2.3看看我的例子...

try { 
      Criteria criteria = new Criteria(); 
      mLocationManagerHelper.SetLocationManager((LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE)); 
      mLocationManagerHelper.GetLocationManager().requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, mLocationManagerHelper.GetLocationListener()); 

      String provider = mLocationManagerHelper.GetLocationManager().getBestProvider(criteria, false); 
      Location location = mLocationManagerHelper.GetLocationManager().getLastKnownLocation(provider); 

      if (location != null) { 
       mLongitude = location.getLongitude(); 
       mLatitude = location.getLatitude(); 
      } 
     } catch (Exception ex) { 
      Log.e(TAG, "GPS", ex); 
     } 

位置幫手

public class LocationManagerHelper { 
private static final String TAG = LocationManagerHelper.class.getSimpleName(); 
private Context mContext; 

private LocationManager mLocationManager; 
private GeoUpdateHandler mLocationListener = new GeoUpdateHandler(); 

public LocationManagerHelper(Context context) { 
    this.mContext = context; 
} 


public GeoUpdateHandler GetLocationListener() { 
    return mLocationListener; 
} 

public void SetLocationManager(LocationManager locationManager) { 
    mLocationManager = locationManager; 
} 

public LocationManager GetLocationManager() { 
    return mLocationManager; 
} 


public void Stop() { 
    if (mLocationManager != null) { 
     mLocationManager.removeUpdates(mLocationListener); 
    } 
} 

private class GeoUpdateHandler implements LocationListener { 
    @Override 
    public void onLocationChanged(Location loc) { 
     String longitude = "Longitude: " + loc.getLongitude(); 
     Log.v(TAG, longitude); 
     String latitude = "Latitude: " + loc.getLatitude(); 
     Log.v(TAG, latitude); 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 
    } 

    @Override 
    public void onProviderDisabled(String s) { 
    } 
} 

}

+0

你在哪裏根據@ Asaf-Nevo要求爲GPS和NETWORK位置提供者實現了監聽器 – rajpara

+1

wooops不正確的代碼,任何你只需添加另一個監聽器的方式,你自己的習慣之後,你只需像往常一樣使用相同的方式我已經給你看。 – IamStalker