代码之家  ›  专栏  ›  技术社区  ›  jhonnyC

如何在android-java中获得实时车速

  •  2
  • jhonnyC  · 技术社区  · 7 年前

    我正在尝试开发一个速度计应用程序,用于测试目的我已经编写了可以获得速度的代码,但我不确定它是否是实时速度,因为有时我看到有一些延迟,这是我的代码:

    public class MainActivity extends AppCompatActivity {
    
    LocationManager locationManager;
    LocationListener locationListener;
    
     public void startListening() {
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    
            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            if (locationManager != null) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        }
    
    }
    
    
    public void updateLocationInfo(Location location){
    
        TextView speedtextView = (TextView) findViewById(R.id.speedTextView);
    
        speedtextView.setText("Current speed: " + location.getSpeed()*3600/1000);
    
     }
    
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                startListening();
    
            }
    
        }}
    
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
    
                updateLocationInfo(location);
    
            }
    
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
    
            }
    
            @Override
            public void onProviderEnabled(String s) {
    
            }
    
            @Override
            public void onProviderDisabled(String s) {
    
            }
        };
    
    
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    
        } else {
    
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    
            if (location != null) {
    
                updateLocationInfo(location);
    
            }
    
        }
    
    }
    }
    

    我还要做什么吗?有没有什么特别的方法来获得实时速度?因为我使用了很多实时速度的应用程序,现在我想知道他们是怎么做到的?可能是“公平”的GPS连接吗?还是我做错了?

    0 回复  |  直到 7 年前