【Android架構GPS篇】之GPS定位應用層流程
一直想搞明白定位資料傳輸從GPS模組到應用層APK的整個流程:Linux串列埠驅動、Android HAL、Android Framework、最終應用程式,同時也瞭解下每個層次都對資料做了什麼限制與手腳!
這裡先了解下應用層流程。
根據這個框架,GPS在應用層實現的最基本流程示例:
[java] view plain copy print?- publicclass MainActivity extends Activity {
- private LocationManager mLocationManager;
- @Override
-
protected
- super.onDestroy();
- mLocationManager.removeUpdates(locationListener);
- }
- @Override
- publicvoid 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() {
- publicvoid 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
- publicvoid onLocationChanged(Location location) {
- updateLocationMessage(location);
- }
- @Override
- publicvoid 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
- publicvoid onProviderEnabled(String provider) {
- }
- @Override
- publicvoid onProviderDisabled(String provider) {
- }
- };
- privatevoid 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位置資訊改變時的通知 |