【发布时间】:2015-08-25 05:56:48
【问题描述】:
我正在按照 Android Developer 上的指南检索位置,并试图从中获得速度。我按照指南编写了这段代码,但日志没有打印,文本也没有更新。
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);
}
}
});
【问题讨论】:
-
你的
onLocationChanged(..)方法被调用了吗? -
你更新清单了吗?
-
是的,我都添加了