如果开发GPS应用方面的软件
①先要获取一个管理器:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
②再定义一个监听:
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
// GPS状态为可见时
case LocationProvider.AVAILABLE:
Log.e("TAG", "当前GPS状态为可见状态");
et01.setText("LocationProvider.AVAILABLE");
break;
// GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:
Log.e("TAG", "当前GPS状态为服务区外状态");
et01.setText("LocationProvider.OUT_OF_SERVICE");
break;
// GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.e("TAG", "当前GPS状态为暂停服务状态");
et01.setText("LocationProvider.TEMPORARILY_UNAVAILABLE");
break;
}
} @Override
public void onProviderEnabled(String provider) {
Log.e("TAG", "onProviderEnabled");
et01.setText("onProviderEnabled");
} @Override
public void onProviderDisabled(String provider) {
Log.e("TAG", "onProviderDisabled");
et01.setText("onProviderDisabled");
} @Override
public void onLocationChanged(Location location) {
// updateView(location);
Log.e("TAG", "时间:" + location.getTime());
Log.e("TAG", "经度:" + location.getLongitude());
Log.e("TAG", "纬度:" + location.getLatitude());
Log.e("TAG", "海拔:" + location.getAltitude());
et01.setText("时间:" + location.getTime() + "经度:" + location.getLongitude() + "纬度:"
+ location.getLatitude() + "海拔:" + location.getAltitude());
}
};
③邦定监听到管理器上(provider直接使用GPS_PROVIDER,不考虑NETWORK_PROVIDER):
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);问题是,测试时(真实设备非虚拟设备),似乎永远无法触发locationListener的onStatusChanged方法,请教高手,何时能触发这个方法,再就解释一下方法中的各个状态,多谢!!!
①先要获取一个管理器:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
②再定义一个监听:
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
// GPS状态为可见时
case LocationProvider.AVAILABLE:
Log.e("TAG", "当前GPS状态为可见状态");
et01.setText("LocationProvider.AVAILABLE");
break;
// GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:
Log.e("TAG", "当前GPS状态为服务区外状态");
et01.setText("LocationProvider.OUT_OF_SERVICE");
break;
// GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.e("TAG", "当前GPS状态为暂停服务状态");
et01.setText("LocationProvider.TEMPORARILY_UNAVAILABLE");
break;
}
} @Override
public void onProviderEnabled(String provider) {
Log.e("TAG", "onProviderEnabled");
et01.setText("onProviderEnabled");
} @Override
public void onProviderDisabled(String provider) {
Log.e("TAG", "onProviderDisabled");
et01.setText("onProviderDisabled");
} @Override
public void onLocationChanged(Location location) {
// updateView(location);
Log.e("TAG", "时间:" + location.getTime());
Log.e("TAG", "经度:" + location.getLongitude());
Log.e("TAG", "纬度:" + location.getLatitude());
Log.e("TAG", "海拔:" + location.getAltitude());
et01.setText("时间:" + location.getTime() + "经度:" + location.getLongitude() + "纬度:"
+ location.getLatitude() + "海拔:" + location.getAltitude());
}
};
③邦定监听到管理器上(provider直接使用GPS_PROVIDER,不考虑NETWORK_PROVIDER):
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);问题是,测试时(真实设备非虚拟设备),似乎永远无法触发locationListener的onStatusChanged方法,请教高手,何时能触发这个方法,再就解释一下方法中的各个状态,多谢!!!
1:你在项目清单文件中是否添加了相应的权限
2:在室内是永远获取到GPS的,请把真实设备放到室外测试