就像 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) {
}
}
}