我正在遵循Android开发人员的指南来检索位置,我正试图从它那里获得速度。我按照指南编写了这段代码,但是日志没有打印,文本也没有更新。
final Button button = (Button) findViewById(R.id.button3);
final TextView speed = (TextView) findViewById(R.id.textView5);
// Acquire a reference to the system Location Manager
final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
final String locationProvider = LocationManager.GPS_PROVIDER;
// Define a listener that responds to location updates
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
float getS=(location.getSpeed());
speed.setText("Speed: "+getS);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("",""+status+" "+provider);
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (running == 0) {
running = 1;
button.setText("Stop measuring");
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
} else {
running = 0;
button.setText("Start measuring");
locationManager.removeUpdates(locationListener);
}
}
});发布于 2015-08-25 14:04:45
在代码中似乎没有问题。您正在使用GPS_PROVODER获取位置。从GPS.You获取位置可能会有问题,应该首先验证GPS在您的设备上是否正常工作。你可以通过使用谷歌地图应用程序并在移动设备时检查位置来完成此操作。
https://stackoverflow.com/questions/32196512
复制相似问题