我正在创建一个应用程序,将手机位置发送到我的服务器。我的问题是每次启动应用程序时手机都给我完全相同的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)
纬度和经度对于每个log cat post都是相同的,并且时间(自1980年左右以来的毫秒数)也是相同的。
好像我的应用程序获取了一次该位置,并且不再尝试获得该位置。
答案 0 :(得分:0)
尝试检查设备上是否启用了GPS,或设备是否支持GPS。