Android location code

Source: Internet
Author: User

No nonsense, on the code. This code is a simple encapsulation of the location function.

Package arui.csdn.net;

Import static android. provider. settings. action_location_source_settings; import android. app. activity; import android. content. context; import android. content. intent; import android. location. location; import android. location. locationlistener; import android. location. locationmanager; import android. OS. bundle; public class locationutil {public static Boolean isenabled (context) {locationmanager mlocationmanager = (locationmanager) context. getsystemservice (context. location_service); If (mlocationmanager. isproviderenabled (locationmanager. gps_provider) {return true;} If (mlocationmanager. isproviderenabled (locationmanager. network_provider) {return true;} return false;} public static void startlocationservice (context) {locationmanager mlocationmanager = (locationmanager) context. getsystemservice (context. location_service); mlocationmanager. requestlocationupdates (locationmanager. gps_provider, 1000l, 10, mlocationlistener); mlocationmanager. requestlocationupdates (locationmanager. network_provider, 1000l, 10, mlocationlistener);} public static void stoplocationservice (context) {locationmanager mlocationmanager = (locationmanager) context. getsystemservice (context. location_service); mlocationmanager. removeupdates (mlocationlistener);} public static location getlocationbygps (context) {locationmanager mlocationmanager = (locationmanager) context. getsystemservice (context. location_service); location = mlocationmanager. getlastknownlocation (locationmanager. gps_provider); Return location;} public static location getlocationbynetwork (context) {locationmanager mlocationmanager = (locationmanager) context. getsystemservice (context. location_service); location = mlocationmanager. getlastknownlocation (locationmanager. network_provider); Return location;} public static void openlocationsetting (Activity Act, int requestcode) {act. startactivityforresult (new intent (action_location_source_settings), requestcode);} Private Static final locationlistener mlocationlistener = new locationlistener () {public void onlocationchanged (location) {// This function is triggered when the coordinates change. If the provider transmits the same coordinates, it will not be triggered by system. out. println ("onlocationchanged:" + location. getlatitude ();} public void onproviderdisabled (string provider) {// This function is triggered when the provider is disable. For example, if GPS is disabled, system is disabled. out. println ("onproviderdisabled:" + provider);} public void onproviderenabled (string provider) {// This function is triggered when the provider is enabled. For example, if GPS is enabled for system. out. println ("onproviderenabled:" + provider);} public void onstatuschanged (string provider, int status, bundle extras) {// This function is triggered when the provider's transition state is available, temporarily unavailable, and no service is directly switched. out. println ("onstatuschanged:" + status );}};}

You need to add permissions:

<Uses-Permission
Android: Name = "android. Permission. access_fine_location"/>

<Uses-Permission
Android: Name = "android. Permission. access_coarse_location"/>

 

---------------------------------------------------------------------------

GL (arui319)

Http://blog.csdn.net/arui319

<This article can be reproduced, but please keep the above author information. Thank you.>

---------------------------------------------------------------------------

 

 

 

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.