您好我有一个应该使用LocationListener获取当前用户位置的类
public class GpsSender {
static Context ctx;
static LocationManager lm;
static Location location;
static double longitude;
static double latitude;
static LocationListener locationListener;
public GpsSender(Context ctx) {
this.ctx = ctx;
lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("LocationListener","Location changed!");
longitude = location.getLongitude();
latitude = location.getLatitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
static public void printLoc() {
Log.i("TEST", "lat:"+latitude+" lon:"+longitude);
}
}
它适用于模拟器,我通过控制台设置lat lon,但是当我在真实设备上尝试时,lat lon始终为0.00。 onLocationChanged,甚至都不会发生。