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.>
---------------------------------------------------------------------------