歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 【Android】GPS啟動流程及數據流向分析(基於2.3.5)

【Android】GPS啟動流程及數據流向分析(基於2.3.5)

日期:2017/3/1 10:35:47   编辑:Linux編程

GPS啟動流程及數據流向分析:

首先在系統init階段,會通過ServiceManager addService添加很多的Service,這其中就包含LocationService。
代碼在SystemServer.java中:

[java]
  1. try {
  2. Slog.i(TAG, "Location Manager");
  3. location = new LocationManagerService(context);
  4. ServiceManager.addService(Context.LOCATION_SERVICE, location);
  5. } catch (Throwable e) {
  6. reportWtf("starting Location Manager", e);
  7. }

隨後調用LocationManagerService的systemReady函數開啟一個線程。

[java]
  1. final LocationManagerService locationF = location;
  2. try {
  3. if (locationF != null) locationF.systemReady();
  4. } catch (Throwable e) {
  5. reportWtf("making Location Service ready", e);
  6. }


--LocationManagerService.java

[java]
  1. void systemReady() {
  2. // we defer starting up the service until the system is ready
  3. Thread thread = new Thread(null, this, "LocationManagerService");
  4. thread.start();
  5. }

在 Thread的run函數中為接收消息做好了准備,並且調用了一個initialize函數:

[java]
  1. public void run()
  2. {
  3. Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
  4. Looper.prepare();
  5. mLocationHandler = new LocationWorkerHandler();
  6. initialize();
  7. Looper.loop();
  8. }

接著看initialize():

[java]
  1. private void initialize() {
  2. // Create a wake lock, needs to be done before calling loadProviders() below
  3. PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
  4. mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
  5. // Load providers
  6. loadProviders();
  7. // Register for Network (Wifi or Mobile) updates
  8. IntentFilter intentFilter = new IntentFilter();
  9. intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
  10. // Register for Package Manager updates
  11. intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  12. intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
  13. intentFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
  14. mContext.registerReceiver(mBroadcastReceiver, intentFilter);
  15. IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
  16. mContext.registerReceiver(mBroadcastReceiver, sdFilter);
  17. // listen for settings changes
  18. ContentResolver resolver = mContext.getContentResolver();
  19. Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
  20. "(" + Settings.System.NAME + "=?)",
  21. new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
  22. null);
  23. mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mLocationHandler);
  24. SettingsObserver settingsObserver = new SettingsObserver();
  25. mSettings.addObserver(settingsObserver);
  26. }
Copyright © Linux教程網 All Rights Reserved