<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
具体实现代码如下:
首先判断GPS模块是否存在或者是开启:
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(MainActivity.this, "请开启GPS导航...",
Toast.LENGTH_SHORT).show();
// 返回开启GPS导航设置界面
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
return;
}
如果开启正常,则会直接进入到显示页面,如果开启不正常,则会进行到GPS设置页面:
返回查询条件
private Criteria getCriteria() {
Criteria criteria = new Criteria();
// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 设置是否要求速度
criteria.setSpeedRequired(false);
// 设置是否允许运营商收费
criteria.setCostAllowed(false);
// 设置是否需要方位信息
criteria.setBearingRequired(false);
// 设置是否需要海拔信息
criteria.setAltitudeRequired(true);
// 设置对电源的需求
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
private LocationListener locationListener = new LocationListener() {
/**
* 位置信息变化时触发
*/
public void onLocationChanged(Location location) {
updateView(location);
}
/**
* GPS状态变化时触发
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
// GPS状态为可见时
case LocationProvider.AVAILABLE:
Toast.makeText(MainActivity.this, "当前GPS状态为可见状态",
Toast.LENGTH_LONG).show();
break;
// GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(MainActivity.this, "当前GPS状态为服务区外状态",
Toast.LENGTH_LONG).show();
break;
// GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(MainActivity.this, "当前GPS状态为暂停服务状态",
Toast.LENGTH_LONG).show();
break;
}
}
/**
* GPS开启时触发
*/
public void onProviderEnabled(String provider) {
Location location = locationManager.getLastKnownLocation(provider);
updateView(location);
}
/**
* GPS禁用时触发
*/
public void onProviderDisabled(String provider) {
updateView(null);
}
};
private GpsStatus.Listener listener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
switch (event) {
// 第一次定位
case GpsStatus.GPS_EVENT_FIRST_FIX:
Log.i(TAG, "第一次定位");
break;
// 卫星状态改变
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Log.i(TAG, "卫星状态改变");
// 获取当前状态
GpsStatus gpsStatus = locationManager.getGpsStatus(null);
// 获取卫星颗数的默认最大值
int maxSatellites = gpsStatus.getMaxSatellites();
// 创建一个迭代器保存所有卫星
Iterator<GpsSatellite> iters = gpsStatus.getSatellites()
.iterator();
int count = 0;
while (iters.hasNext() && count <= maxSatellites) {
iters.next();
count++;
}
System.out.println("搜索到:" + count + "颗卫星");
break;
// 定位启动
case GpsStatus.GPS_EVENT_STARTED:
Log.i(TAG, "定位启动");
break;
// 定位结束
case GpsStatus.GPS_EVENT_STOPPED:
Log.i(TAG, "定位结束");
break;
}
};
};
private void updateView(Location location) {
if (location != null) {
editText.setText("设备位置信息\n\n经度:");
editText.append(String.valueOf(location.getLongitude()));
editText.append("\n纬度:");
editText.append(String.valueOf(location.getLatitude()));
editText.append("\n海拔:");
editText.append(String.valueOf(location.getAltitude()));
} else {
editText.getEditableText().clear();
}
}