4
我正在創建一個應用程序以將電話位置發送到我的服務器。我遇到的問題是,每次啓動應用程序時,手機都會給出完全相同的GPS座標。當我重新啓動它時,座標可以改變,但是在發送第一個座標之後,它將發送相同的內容,直到應用程序重新啓動。Android位置管理器每次都返回相同的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年左右。)是一樣的。
看起來像我的應用程序獲取位置一次,並沒有試圖再次獲得位置。
你在用三星手機嗎? – Siddharth
你有答案嗎?我也有同樣的問題。 –
有沒有更新?你以前試過解決方案嗎? – Siddharth