很簡單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) {
}
}
}
如果你之前先閱讀該文件將是巨大的在這裏問。 http://developer.android.com/guide/topics/location/strategies.html – rajpara
是的,你可以,但你需要管理提供者的準確性,當然你要怎麼做纔是你的。同時設置提供程序以儘可能快地發送更新將消耗大量的功率,因此您可能需要考慮是否真的需要這些功能。 – Idistic
另請查看[本教程出處](http://www.anddev.org/novice-tutorials-f8/gps-and-network-location-t16563.html) – jnthnjns