I debugged the GPS function on Android today and found many articles on the Internet. The method is as follows:
Locationmanager = (locationmanager) Context. getsystemservice (context. location_service); // initialize locationmanagerif (! Locationmanager. isproviderenabled (Android. location. locationmanager. gps_provider) // checks whether the GPS function is enabled. For example, network_provider location = locationmanager. getlastknownlocation (Android. location. locationmanager. gps_provider); // obtain location using this sentence
The most important thing is to set the locationlistener so that you can monitor the status of the latest location update and location functions at any time:
Locationmanager. requestlocationupdates (Android. Location. locationmanager. gps_provider, 5, locationlistener, logoff. mylogoff (); // The second and third parameters set the update frequency.
Locationlistener variable declaration:
Private Final locationlistener = new locationlistener () {// rewrite the following four methods as needed: Public void onlocationchanged (location LOC) {} // call Public void onproviderdisabled (string p) {} // call Public void onproviderenabled (string p) after the provider is enabled by the user) {}// call Public void onstatuschanged (string provider, int status, bundle extras) when the provider status changes ){}};
Pass
Locationmanager. removeupdates (locationlistener); // Delete listener, because it is very expensive to keep on
Note:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
For details, refer:
Http://blog.csdn.net/sam_zhang1984/article/details/6569030
Http://www.learningandroid.net/blog/foundation/tutorial-location-service/
I used to call this GPS class in the new thread, and I am not familiar with logoff, so I have been debugging for a long time. Note that you need to use it when registering locationlistener in a child thread.
Requestlocationupdates (string provider, long mintime, float mindistance, locationlistener listener, low.logoff) method. logoff needs to call the lorule. Prepare () method first, and then call the lorule. Loop () method. As follows:
Logoff. prepare (); // equivalent to the initialization locationmanager of the thread logoff. requestlocationupdates (Android. location. locationmanager. gps_provider, 60000,5, locationlistener, logoff. myloop (); // obtain the looperloop of the thread. loop (); // logoff starts execution. Note that after the statement is executed, other operations in this thread will be blocked and only respond to events.