【Android架构GPS篇】之GPS定位应用层流程

一直想搞明白定位数据传输从GPS模块到应用层APK的整个流程:Linux串口驱动、Android HAL、Android Framework、最终应用程序,同时也了解下每个层次都对数据做了什么限制与手脚!

这里先了解下应用层流程。

【Android架构GPS篇】之GPS定位应用层流程_第1张图片


根据这个框架,GPS在应用层实现的最基本流程示例:

public class MainActivity extends Activity {
	private LocationManager mLocationManager;

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mLocationManager.removeUpdates(locationListener);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		/* 记得在AndroidManifest.xml文件中开启GPS相关的权限!!! */
		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		/* 检测GPS定位模块是否开启 */
		if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			/* 针对GPS定位模块是否开启,具体接下来做的事 */
			return;
		}

		Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		updateLocationMessage(location);
		
		/* 监听GPS的状态变化 */
		mLocationManager.addGpsStatusListener(listener);
		
		/* 监听GPS的位置变化
		 * 这里指定2000ms或者移动距离超过4m的时候更新一次位置信息,但是
		 * 经过实际测试,更新间隔精确度极低,根本不按套路走。实际使用的话,还是采用Send Measage方式
		 */
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 4, locationListener);
	}

	/* 监听GPS的状态变化 */
	GpsStatus.Listener listener = new GpsStatus.Listener() {
		public void onGpsStatusChanged(int event) {
			switch (event) {
			/* 第一次获取到定位信息 */
			case GpsStatus.GPS_EVENT_FIRST_FIX:
				break;
			/* 卫星状态发生变化,捕获到卫星/卫星不可见 */
			case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
				break;
			case GpsStatus.GPS_EVENT_STARTED:
				break;
			case GpsStatus.GPS_EVENT_STOPPED:
				break;
			}
		};
	};
	
	private LocationListener locationListener = new LocationListener() {
		@Override
		public void onLocationChanged(Location location) {
			updateLocationMessage(location);
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			switch (status) {
			case LocationProvider.AVAILABLE:
				break;
			case LocationProvider.OUT_OF_SERVICE:
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE:
				break;
			}
		}

		@Override
		public void onProviderEnabled(String provider) {
			
		}

		@Override
		public void onProviderDisabled(String provider) {
			
		}

	};

	private void updateLocationMessage(Location location) {
		
	}
}

上面提到的是GPS最基础的框架流程,此外它还有你想得到、想不到的其他许多用法与功能。

在Android的location包中,所有与定位相关的类和接口如下:

 

 

 

 

 

 

 

 

 

 

Address

representing an Address, i.e, a set of Strings describing a location

描述地址信息

Criteria

indicating the application criteria for selecting a location provider

根据自己要求,选择LocationProvider

Geocoder

handling geocoding and reverse geocoding

处理地理位置信息的编码

GpsSatellite

representing the current state of a GPS satellite

描述GPS卫星当前状态

GpsStatus

representing the current state of the GPS engine

描述GPS设备的当前状态

Location

representing a geographic location sensed at a particular time

描述地理位置信息,如经度、纬度、高度、方向、运动速度等

LocationManager

provideing access to the system location services

用于调用、管理系统定位服务,是整个定位服务的入口、核心

LocationProvider

An abstract superclass for location providers. A location provider provides periodic reports on the geographical location of the device

描述location providers的抽象超类,是真正用来获取位置信息的

 

 

 

接口

GpsStatus.Listener

 

receiving notifications when GPS status has changed

接收GPS状态改变时的通知

GpsStatus.

NmeaListener

receiving NMEA sentences from the GPS

接收GPS的NMEA信息

LocationListener

receiving notifications from the LocationManager when the location has changed

接收GPS位置信息改变时的通知

你可能感兴趣的:(Android源码)