2012-08-31 47 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