Android GPS数据上报线程的开启流程


接下来就该讨论JNI(java部分)(GpsLocationProvider.java)层的数据传输了,在JNI(java部分)层中,JNI(c/c++)层的函数Android_location_GpsLocationProvider_wait_for_event被映射成函数native_wait_for_event,其在:
    private final class GpsEventThread extends Thread {
        public GpsEventThread() {
            super("GpsEventThread");
        }
        public void run() {
            if (DEBUG) Log.d(TAG, "GpsEventThread starting");
            // Exit as soon as disable() is called instead of waiting for the GPS to stop.
            while (mEnabled) {
                // this will wait for an event from the GPS,
                // which will be reported via reportLocation or reportStatus
                native_wait_for_event();             //接受冲JNI层穿上来的数据
            }
            if (DEBUG) Log.d(TAG, "GpsEventThread exiting");
        }
    }
继续跟踪可发现,这个现在
    private void handleEnable() {
        if (DEBUG) Log.d(TAG, "handleEnable");
        if (mEnabled) return;
        mEnabled = native_init();
        if (mEnabled) {
            if (mSuplServerHost != null) {
                native_set_agps_server(AGPS_TYPE_SUPL, mSuplServerHost, mSuplServerPort);
            }
            if (mC2KServerHost != null) {
                native_set_agps_server(AGPS_TYPE_C2K, mC2KServerHost, mC2KServerPort);
            }
            // run event listener thread while we are enabled
            mEventThread = new GpsEventThread(); //创建线程并运行
            mEventThread.start();
        } else {
            Log.w(TAG, "Failed to enable location provider");
        }
    }
中被创建,并运行,而这个函数有是在
private final class ProviderHandler extends Handler {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what) {
                case ENABLE:
                    if (msg.arg1 == 1) {
                        handleEnable();//调用
                    } else {
                        handleDisable();
                    }
                    break;
 。。。省略。。。
  }
 。。。省略。。。
 }
}
被调用,接着这个句柄,是在
   private final class GpsLocationProviderThread extends Thread {
        public GpsLocationProviderThread() {
            super("GpsLocationProvider");
        }
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            initialize();
            Looper.prepare();
            mHandler = new ProviderHandler();
            // signal when we are initialized and ready to go
            mInitializedLatch.countDown();
            Looper.loop();
        }
    }
这个线程中被创建使用,这个线程又在GpsLocationProvider中被创建并运行使得数据开始上报:
public GpsLocationProvider(Context context, ILocationManager locationManager){
。。。省略。。。
mThread = new GpsLocationProviderThread();
        mThread.start();
。。。省略。。。
}
这个函数是类GpsLocationProvider的构造函数,此构造函数将在server(LocationManagerService.java)中的_loadProvidersLocked函数被调用。

相关阅读:Android GPS数据上报(基于gps_qemu.c)

相关内容