一直想搞明白定位資料傳輸從GPS子產品到應用層APK的整個流程:Linux序列槽驅動、Android HAL、Android Framework、最終應用程式,同時也了解下每個層次都對資料做了什麼限制與手腳!
這裡先了解下應用層流程。

根據這個架構,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位置資訊改變時的通知 |