我正在创建一个应用程序来将手机位置发送到我的服务器。我遇到的问题是,每次我启动应用程序时,手机都会给我完全相同的 gps 坐标。当我重新启动它时,坐标可能会改变,但是在发送第一个坐标后,它会发送一次相同的坐标,直到重新启动应用程序。
代码:
// Contructor etc
public class LocationASYNC extends AsyncTask<Activity, String, Void> {
private MainActivity m;
private LocationManager locationManager;
private Criteria c;
public LocationASYNC(MainActivity m) {
this.m = m;
locationManager = (LocationManager) m
.getSystemService(Context.LOCATION_SERVICE);
c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 0, m);
}
// Code for finding best location provider and returning location
private Location findBestLocation() {
Location location = locationManager
.getLastKnownLocation(locationManager.getBestProvider(c, true));
Log.v("location",
"provider: " + locationManager.getBestProvider(c, true));
// Check if location is null
if (location != null) {
Log.v("location", "provider not null");
// Test if location is more than 100 seconds old
if (location.getTime() < (System.currentTimeMillis() - 100000)) {
Log.v("location", "provider is old(GPS)");
// Use network if location is more than 100 seconds (99% chance
// location will be gps)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// Check if network location is more then 100 seconds
Log.v("location", "provider: " + location.getProvider());
if (location.getTime() < (System.currentTimeMillis() - 100000)) {
// Return null if all location data is old
Log.v("location", "provider is old (INT)");
return null;
}
}
// Return location if one is less than 100 seconds old
return location;
}
// return null if no location is found
return null;
}
This is the output i get from logcat:
02-05 15:31:59.670: V/location(29659): provider: network
02-05 15:31:59.675: V/location(29659): lat: 60.3935485 lon: 5.3114676 time: 1360074627953 acc: 28.757
02-05 15:32:02.685: V/location(29659): provider: gps
02-05 15:32:02.685: V/location(29659): provider not null
02-05 15:32:02.685: V/location(29659): provider is old(GPS)
02-05 15:32:02.690: V/location(29659): provider: network
02-05 15:32:02.690: V/location(29659): lat: 60.3935485 lon: 5.3114676 time: 1360074627953 acc: 28.757
02-05 15:32:05.700: V/location(29659): provider: gps
02-05 15:32:05.700: V/location(29659): provider not null
02-05 15:32:05.705: V/location(29659): provider is old(GPS)
02-05 15:32:05.710: V/location(29659): provider: network
02-05 15:32:05.710: V/location(29659): lat: 60.3935485 lon: 5.3114676 time: 1360074627953 acc: 28.757
02-05 15:32:08.720: V/location(29659): provider: gps
02-05 15:32:08.720: V/location(29659): provider not null
02-05 15:32:08.720: V/location(29659): provider is old(GPS)
02-05 15:32:08.725: V/location(29659): provider: network
02-05 15:32:08.725: V/location(29659): provider is old (INT)
每个日志猫帖子的纬度和经度都相同,时间(自 1980 年左右以来的毫秒)也是相同的。
似乎我的应用程序获得了一次位置,并且没有尝试再次获得该位置。