【发布时间】:2014-05-21 19:28:23
【问题描述】:
我有一个每分钟都在运行的服务,并且应该不断跟踪位置。该服务实现了 LocationListener。我觉得我没有正确理解这一点,因为我实际上并没有使用 LocationListener 方法: onLocationChanged onProviderEnabled onProviderDisabled 等等
我的班级中有这些方法,但我实际上并没有对它们做任何事情。我所做的只是每次我的服务运行时,我都会根据可用的 GPS 和网络提供商调用 LocationManager.getLastKnownLocation。
这是我的代码的样子:
服务每分钟运行一次:
Intent i = new Intent(context, GPSTracker.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if(isOn) {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), POLL_INTERVAL, pi);
} else {
alarmManager.cancel(pi);
pi.cancel();
}
我处理 Intent:
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "In onHandleIntent!");
Location currentLocation = getLocation(this);
if(currentLocation == null) {
saveLocation(-8.0, -8.0, "nothing", "nothing", "nothing");
} else {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String city = addresses.get(0).getLocality();
String country = addresses.get(0).getCountryCode();
String state = addresses.get(0).getAdminArea();
saveLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), city, state, country);
} catch (IOException e) {
e.printStackTrace();
}
}
}
getLocation() 才是真正的工作:
public Location getLocation(Context context) {
try {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
但似乎有些不对劲。一方面,就像我说的那样,我不使用任何 LocationListener 方法,而且它似乎也不是很准确。当我坐在电脑桌前用手机进行测试时,它每次都能找到它。然而,今天我有一次在工作时在我的手机上运行这个程序,还有一次当我走在街上时,这些地方的位置距离它们都只有一英里远。奇怪的是,这两个地方的位置完全相同。因为我没有打开 GPS,所以这两个都是网络提供商。
我做错了什么,如果我没有做错什么,我怎样才能更准确?
【问题讨论】:
-
你从哪里得到这个代码???我认为这段代码是我写的:P:P
-
为了您的信息,来自 GPS 的位置差异很大。所以你需要完全消除那些不适合你的点。
-
我从这个博客得到代码:niravranpara.blogspot.se/2013/04/…
-
是的,我知道它变化很大,但我认为我的主要问题是,在博客文章中,它不是在后台服务中运行,而是在应用程序旁边运行。即使应用程序不存在,我也需要我的工作。
-
您可以轻松地将代码从 Activity 移动到服务。我没有确切的代码,但我有一个代码可以定期将用户的位置发送到服务器。如果您需要代码,请将您的电子邮件发送给我。
标签: android gps location locationlistener