Call the GPS function in the android sub-Thread

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.