Extended battery life--positioning

Source: Internet
Author: User

Android offers a variety of location services:

(1) GPS (Global Positioning system using satellites);

(2) Network (base station location with Cell-id, Wi-Fi service address)

(3) Passive (API Level 8 added)

In the absence of special requirements, you can set the frequency of updates and reduce the number of requests. Register Listener, disable location monitoring when not in use

Accept Location Updates

private void Requestlocationupdates () {Locationmanager LM = (locationmanager) getsystemservice (context.location_ SERVICE); List<string> Provider = Lm.getallproviders (); if (provider! = NULL &&!provider.isempty ()) { Locationlistener listener = new Locationlistener () {@Overridepublic void onstatuschanged (String provider, int status, Bundle extras) {//Todo auto-generated method stub} @Overridepublic void Onproviderenabled (String provider) {//Todo auto-g enerated method stub} @Overridepublic void Onproviderdisabled (String provider) {//TODO auto-generated method stub}@ overridepublic void onlocationchanged (location location) {//TODO auto-generated method Stub}};for (String Name:provider {lm.requestlocationupdates (name, Dateutils.hour_in_millis * 1,100, listener);//Set the update frequency}}}//Logoff listener (the user can force a location to be fixed), Call private void Disablelocationlistener (Locationlistener listener) {Locationmanager LM = (locationmanager) where it needs to be called Getsystemservice (Context.location_service); Lm.removeupdates (listener);} Last known location (no need to start location services) Private Locations getlastknownlocation () {Locationmanager LM = (Locationmanager) getsystemservice ( Context.location_service); list<string> names = Lm.getallproviders (); Location Location=null;if (names!=null) {for (String name:names) {if (! LocationManager.PASSIVE_PROVIDER.equals (name)) {location l=lm.getlastknownlocation (name); if ((L!=null) && ( location==null| | L.gettime () >location.gettime ())) {location=l;}}} return location;}


Extended battery life--positioning

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.