Recently, GPS is being used on Android. It is always difficult to get up or down, no data, and debugging is in progress. Therefore, we carefully analyzed the code from top to bottom. The soul function is
The requestlocationupdates function is mainly used to start a monitor and listen for location change information.
Not much nonsense. Start analysis.
1. First find the location of the requestlocationupdates Function
/Android_work_path/frameworks/base/location/Java/Android/location/locationmanager. Java
Public void requestlocationupates (string provider, <br/> long mintime float mindistance, locationlistener listener) {</P> <p>... <br/> _ requestlocationupdates (provider, mintime, mindistance, listener); // core function <br/>... </P> <p>}
This function is used to determine whether the provider parameter is null. If it is not null, call the core function, which is easy and difficult.
Continue searching in this function
Private void _ requestlocationupdates (string provider, <br/> long mintime, float mindistance, locationlistener listener, <br/> Looper lostance) {<br/> If (mintime <0l) {<br/> mintime = 0l; <br/>}< br/> If (mindistance <0.0f) {<br/> mindistance = 0.0f; <br/>}</P> <p> try {<br/> synchronized (mlisteners) {<br/> listenertransport transport = mlisteners. get (listener); // core function <br/> If (Transport = NULL) {<br/> transport = new listenertransport (listener, Loener ); <br/>}< br/> mlisteners. put (listener, transport); <br/> mservice. requestlocationupdates (provider, mintime, mindistance, transport); <br/>}< br/>} catch (RemoteException ex) {<br/> log. E (TAG, "requestlocationupdates: deadobjectexception", ex); <br/>}< br/>
At this time, the code will trigger a part of listener separately, and some will perform the provider action (gpslocation)
First talk about the gps_provider action
The main functions are:/location/Java/COM/Android/Internal/location/gpslocationprovider. java.
Public gpslocationprovider (context, <br/> ilocationmanager locationmanager) {<br/>... <br/> mthread = new gpslocationproviderthread (); <br/> mthread. start (); <br/>... </P> <p>}
Start a program
Private final class gpslocationproviderthread extends thread {</P> <p> Public gpslocationproviderthread () {<br/> super ("gpslocationprovider "); <br/>}</P> <p> Public void run () {<br/> process. setthreadpriority (process. thread_priority_background); <br/> initialize (); <br/> loaliz. prepare (); <br/> mhandler = new providerhandler (); // core <br/> // signal when we are initialized and ready to go <br/> minitializedlatch. countdown (); <br/> logoff. loop (); <br/>}< br/>}
Create a providerhandle
Private final class providerhandler extends handler {<br/> @ override <br/> Public void handlemessage (Message MSG) <br/>{< br/> switch (MSG. what) {<br/> case enable: <br/> If (MSG. arg1 = 1) {<br/> handleenable (); <br/>}else {<br/> handledisable (); <br/>}< br/> break; <br/> case enable_tracking: <br/> handleenablelocationtracking (MSG. arg1 = 1); <br/> break; <br/>... <br/>}< br/>}
If this is the first time you start initialization, It is enable.
Enable_tracking
The following uses enable_tracking as an example:
Will execute the handleenablelocationtracking Function
Private void handleenablelocationtracking (Boolean enable) {<br/> If (enable) {<br/> mttff = 0; <br/> mlastfixtime = 0; <br/> startnavigating (); <br/>} else {<br/> malarmmanager. cancel (mwakeupintent); <br/> malarmmanager. cancel (mtimeoutintent); <br/> stopnavigating (); <br/>}</P> <p>}
The starnavigating () function is important to continue tracking.
Private void startnavigating () {<br/> If (! Mstarted) {<br/> If (Debug) log. D (TAG, "startnavigating"); <br/> mstarted = true; <br/> int positionmode; <br/> If (settings. secure. getint (mcontext. getcontentresolver (), <br/> Settings. secure. received_gps_enabled, 1 )! = 0) {<br/> positionmode = gps_position_mode_ms_based; <br/>}else {<br/> positionmode = gps_position_mode_standalone; <br/>}< br/> // native_stat is implemented by JNI, which is the focus <br/> If (! Native_start (positionmode, false, 1) {<br/> mstarted = false; <br/> log. E (TAG, "native_start failed in startnavigating ()"); <br/> return; <br/>}< br/>
Native_start is implemented in the. cpp file on the JNI layer. Now we can view this CPP file.
Frameworks/base/CORE/JNI/android_location_gpslocationproivider.cpp
Static jboolean android_location_gpslocationprovider_start (jnienv * ENV, jobject OBJ, jint positionmode, <br/> jboolean singlefix, jint fixfrequency) <br/>{< br/> int result = sgpsinterface-> set_position_mode (positionmode, (singlefix? 0: fixfrequency); <br/> If (result) {<br/> return false; <br/>}</P> <p> return (sgpsinterface-> Start () = 0); <br/>}< br/>
Here we do two actions: 1 is set_position_mode 2 is start
The structure prototype of sgpsinterface is gpsinterface.
Some operations are defined in/android_work/hardware/libhardware_legacy/GPS/xxx_gps.c. This layer is what we call the Hardware Abstraction Layer HAL.
Static const gpsinterface xxxgpsinterface ={< br/> xxx_gps_init, <br/> xxx_gps_start, <br/> xxx_gps_stop, <br/> xxx_gps_cleanup, <br/> quit, <br/> xxx_gps_inject_location, <br/> xxx_gps_delete_aiding_data, <br/> xxx_gps_set_position_mode, <br/> xxx_gps_get_extension, <br/> };
You can see the gps_set_position_mode and gps_start functions.
These functions call the. So function written by the chip manufacturer.
For example, if a producer is YYY, then he can import. So in the xxx_gps.c file, and then use his function to operate the hardware.
PS: fuck now. so only part of the source code is exposed ,. if file A is not publicly disclosed, the vendor of my grass will adjust it to it, which will lead to an error. The LSM (location Session Manager) will be suspended, and no code will be provided yet, so I will be confused. wating...