0

アプリで 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 です。足りないものはありますか?

4

1 に答える 1