我想从我的应用程序中的 LocationManager 中获得速度。我有一个带有 setSpeedRequired(true) 属性的条件。我正在做 location.getSPeed(),但它一直给我 0。下面是我作为服务运行的 GPS 的代码。
public class Tracking extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
double latitude;
double longitude;
float velocity;
String provider;
private static final long minDist = 0;
private static final long minTime = 0;
LocationDatabaseHandler ldb;
@Override
public void onCreate() {
ldb = new LocationDatabaseHandler(this);
getLocation();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@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 intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
//because we do not want to stop the service unless we explicitly say so.
return START_STICKY;
}
public Location getLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setSpeedRequired(true);
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDist,
this);
location = locationManager.getLastKnownLocation(provider);
if(location != null) {
onLocationChanged(location);
}
return location;
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
batteryP = getBatteryPerc();
velocity = location.getSpeed();
insertIntoDb(latitude, longitude, velocity, "onchanged");
}
public void insertIntoDb(double latitude, double longitude, float velocity, String where) {
Date date = new Date();
String dateStr = date.toString();
ldb.addLocation(new Locations(latitude, longitude, dateStr, velocity));
}
}
当我看到我的数据库时,速度总是 0.0。有什么我想念的吗?