【发布时间】:2016-12-21 09:58:03
【问题描述】:
我有一个三星 Galaxy Tab A。我已经实现了 android.location.LocationListener 和 onLocationChanged(Location location) 被调用,它给了我正确的经度和纬度但是,
问题是:
location.getTime() 给了我错误的时间,它返回的时间接近 300000 百万左右。它应该类似于1471243684497。
我不知道该怎么办。
GPSTracker 类:
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener
{
//flag for GPS Status
boolean isGPSEnabled = false;
//flag for network status
//boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
//The minimum distance to change updates in metters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 metters
//The minimum time beetwen updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 10; // 1 minute
//Declaring a Location Manager
protected LocationManager locationManager;
/*public GPSTracker(Context context)
{
this.mContext = context;
getLocation();
}*/
public Location getLocation(Context ctx)
{
try
{
locationManager = (LocationManager) ctx.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;
//First get location from Network Provider
/* 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);
updateGPSCoordinates();
}
}*/
//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);
updateGPSCoordinates();
}
}
}
}
}
catch (Exception e)
{
//e.printStackTrace();
Log.e("Error : Location", "Impossible to connect to LocationManager", e);
}
return location;
}
public void updateGPSCoordinates()
{
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
*/
public void stopUsingGPS()
{
if (locationManager != null)
{
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
*/
public double getLatitude()
{
if (location != null)
{
latitude = location.getLatitude();
}
return latitude;
}
/**
* Function to get longitude
*/
public double getLongitude()
{
if (location != null)
{
longitude = location.getLongitude();
}
return longitude;
}
/**
* Function to check GPS/wifi enabled
*/
public boolean canGetLocation()
{
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)/*&&
!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)*/){
this.canGetLocation = false;
} else {
this.canGetLocation = true;
}
return this.canGetLocation;
}
/**
* Get list of address by latitude and longitude
* @return null or List<Address>
*/
public List<Address> getGeocoderAddress(Context context)
{
if (location != null)
{
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
try
{
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
return addresses;
}
catch (IOException e)
{
//e.printStackTrace();
Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e);
}
}
return null;
}
/**
* Try to get AddressLine
* @return null or addressLine
*/
public String getAddressLine(Context context)
{
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0)
{
Address address = addresses.get(0);
String addressLine = address.getAddressLine(0);
return addressLine;
}
else
{
return null;
}
}
/**
* Try to get Locality
* @return null or locality
*/
public String getLocality(Context context)
{
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0)
{
Address address = addresses.get(0);
String locality = address.getLocality();
return locality;
}
else
{
return null;
}
}
/**
* Try to get Postal Code
* @return null or postalCode
*/
public String getPostalCode(Context context)
{
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0)
{
Address address = addresses.get(0);
String postalCode = address.getPostalCode();
return postalCode;
}
else
{
return null;
}
}
/**
* Try to get CountryName
* @return null or postalCode
*/
public String getCountryName(Context context)
{
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0)
{
Address address = addresses.get(0);
String countryName = address.getCountryName();
return countryName;
}
else
{
return null;
}
}
@Override
public void onLocationChanged(Location location)
{
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
还有我自己的扩展 GPSTracker 类的类:
public class NewMainService extends GPSTracker {
@Override
public void onLocationChanged(Location location) {
long time = location.getTime();
Log.i("TAG", "onLocationChanged time: "+time); //prints wrong timestamp
super.onLocationChanged(location);
}
}
编辑:
在其他设备上一切正常。
我用谷歌搜索了它,这是某些设备中的错误。位置侦听器在一段时间后停止接收更改。
它给我的时间就像设备的正常运行时间,是几个小时,而不是日期。
编辑 2:
现在我改用com.google.android.gms.location.FusedLocationProviderClient 而android.location.LocationManager 在Android 10+ 上甚至不再给我任何位置更新
【问题讨论】:
-
遇到了同样的问题(LocationManager 返回设备正常运行时间的位置,而不是自纪元以来的时间) - 如果可能,您能否提供有关该错误的链接?除了这个 SO 问题,找不到任何东西。非常感谢!
-
@AlephAleph,我最终实现了自己的简单位置侦听器,而不是“融合位置”。并将其与 PARTIAL_WAKE_LOCK 类型的唤醒锁混合,我注册了
Locationmanager以监听网络提供商和 gps 提供商。现在它工作正常。 -
我在一些华硕设备上遇到了类似的问题。我正在使用融合位置提供程序。
getTime()方法有时会返回错误一小时的时间。我不更改设备的时间和时区。你找到解决办法了吗? -
在启动位置请求时,onLocationChanged 似乎从某个缓冲区提供了一个旧位置。我总是设置上次修复的时间,并且仅在时间大于上次修复时才花费时间(并且从第一次修复中不花费时间,而仅使用它设置 last_location_time)。我建议也检查供应商,只有在 provider.equals(LocationManager.GPS_PROVIDER); 时才花时间。如果有的话,我认为你不能依赖 wifi 的时间戳。我仍然想知道这是否真的是失败证明,或者我是否应该完全停止使用 gps 时间并完全切换到 SNTP
-
@FrankKrumnow 改用
com.google.android.gms.location.FusedLocationProviderClient
标签: android android-location android-gps