Android, how to change activity field from LocationListener.onlocationchanged()?
How do I change an activity field from LocationListener.onlocationchanged() ? I suppose I have to use a handler, but don't know how to do it, exactly... My code is like this:
public class Main extends Activity {
private Double lat, lon;
private LocationManager locationManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListenerGps = new CurrentLocationGps();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0开发者_开发问答, 0, locationListenerGps);
}
}
public class CurrentLocationGps implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
lat = loc.getLatitude();
lon = loc.getLongitude();
>>>>>>>>---<<<<<<<<< How do I pass this values to Main activity fields?
}
}
}
Place the CurrentLocationGps class inside the Main class (making it an inner class), this way you can access all the fields in Main activity.
精彩评论