Chinaunix首页 | 论坛 | 博客
  • 博客访问: 227517
  • 博文数量: 29
  • 博客积分: 1477
  • 博客等级: 上尉
  • 技术积分: 451
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 14:48
文章分类
文章存档

2012年(1)

2011年(14)

2010年(14)

我的朋友

分类: 嵌入式

2010-12-22 11:03:10

Android GPS架构分析
Daniel Wood 20101222
转载时请注明出处和作者
文章出处:http://danielwood.cublog.cn
作者:Daniel Wood
--------------------------------------------------------------------
   介绍完了主体代码结构以及重要的数据结构后,下面来看看gps的定位服务(LocationManager)的启动过程。我总是喜欢追本溯源地从源头去认识事物。因为“人之初,性本善”,从事物的本性去认识事物。
LocationManager 这项服务是在SystemServer.java 中启动的,也就是系统启动之后,这个服务就已经启动了:

systemServer.java [framework\base\services\java\com\android\server]

SystemServer.javainit2函数中启动了一个线程来注册Android的诸多服务,如:Bluetooth ServiceNetworkManagement ServiceNotification Manager等,当然也包括Location Service

SystemServer.java [frameworks\base\services\java\com\android\server]

public static final void init2() {
        Slog.i(TAG, "Entered the Android system server!");
        Thread thr = new ServerThread();
        thr.setName("android.server.ServerThread");
        thr.start();
}

ServerThread线程的run函数中LocationManager服务的代码段如下:

2.1版本
try {
                Log.i(TAG, "Location Manager");
                ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
            } catch (Throwable e) {
                Log.e(TAG, "Failure starting Location Manager", e);
            }
2.2的代码中代码段如下形式:
       try {
                Slog.i(TAG, "Location Manager");
                location = new LocationManagerService(context);
                ServiceManager.addService(Context.LOCATION_SERVICE, location);
            } catch (Throwable e) {
                Slog.e(TAG, "Failure starting Location Manager

run函数的后半部分,是服务对系统的反馈,就是systemReady()函数。 LocationManager服务的反馈函数如下:

if (locationF != null) locationF.systemReady();

其中的locationF LocationManagerServicefinal类型,就是一旦赋值,不能更改。

final LocationManagerService locationF = location;

哇!locationManager这项服务的反馈机制只在2.2的代码里面才有啊。2.1中的反馈机制中并没有locationManager(当然有其他的服务反馈)。

而在2.1版本中LocationManagerService的构造函数如下:

LocationManagerService.java [frameworks\base\services\java\com\android\server]

public LocationManagerService(Context context) {
        super();
        mContext = context;
        Thread thread = new Thread(null, this, "LocationManagerService");
        thread.start();
        if (LOCAL_LOGV) {
            Log.v(TAG, "Constructed LocationManager Service");
        }
    }


2.2版本

public LocationManagerService(Context context) {
        super();
mContext = context;
        if (LOCAL_LOGV) {
            Slog.v(TAG, "Constructed LocationManager Service");
        }
    }

2.1是在构造函数的时候就启动一个自身服务线程。见构造函数。

2.2是在反馈机制中通过systemReady函数启动自身服务线程。如下:

void systemReady() {
        // we defer starting up the service until the system is ready 
        Thread thread = new Thread(null, this, "LocationManagerService");
        thread.start();
    }

通过线程run函数,调用initialize函数:

public void run()
    {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        Looper.prepare();
        mLocationHandler = new LocationWorkerHandler();
        initialize();
        Looper.loop();
}


阅读(4482) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~