灵敏度Android位置传感器

时间:2016-08-09 11:48:01

标签: android google-maps google-maps-android-api-2 android-sensors android-location

我想获得每10毫秒的位置,但我的位置在大约30秒或40秒内变化。我的错在哪里?有人帮帮我吗?我正在使用此代码块。我也使用过它,但我遇到了一些问题http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mGoogleApiClient.disconnect();
}

synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();


}

@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1); // Update location every second

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);


    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        lat = String.valueOf(mLastLocation.getLatitude());
        lon = String.valueOf(mLastLocation.getLongitude());

    }
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    Toast.makeText(MainActivity.this, location.getLatitude()+"+-+"+location.getLongitude(), Toast.LENGTH_SHORT).show();
    updateUI();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    buildGoogleApiClient();
}
void updateUI() {
    Log.e("Updated",lat+"---"+lon);
}

Android版本6.0.1

1 个答案:

答案 0 :(得分:1)

你可以这样做:

mLocationRequest.setInterval(3000);
mLocationRequest.setFastestInterval(1000); // For 1 Second = 1000 millis