歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android的Location功能代碼

此代碼為Location功能的簡單封裝。

package arui.csdn.net;

 

  1. import static Android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.location.Location;  
  6. import android.location.LocationListener;  
  7. import android.location.LocationManager;  
  8. import android.os.Bundle;  
  9.   
  10. public class LocationUtil {  
  11.   
  12.     public static boolean isEnabled(Context context) {  
  13.         LocationManager mLocationManager = (LocationManager) context  
  14.                 .getSystemService(Context.LOCATION_SERVICE);  
  15.         if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {  
  16.             return true;  
  17.         }  
  18.         if (mLocationManager  
  19.                 .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {  
  20.             return true;  
  21.         }  
  22.         return false;  
  23.     }  
  24.   
  25.     public static void startLocationService(Context context) {  
  26.         LocationManager mLocationManager = (LocationManager) context  
  27.                 .getSystemService(Context.LOCATION_SERVICE);  
  28.         mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,  
  29.                 1000L, 10, mLocationListener);  
  30.         mLocationManager.requestLocationUpdates(  
  31.                 LocationManager.NETWORK_PROVIDER, 1000L, 10,  
  32.                 mLocationListener);  
  33.     }  
  34.   
  35.     public static void stopLocationService(Context context) {  
  36.         LocationManager mLocationManager = (LocationManager) context  
  37.                 .getSystemService(Context.LOCATION_SERVICE);  
  38.         mLocationManager.removeUpdates(mLocationListener);  
  39.     }  
  40.   
  41.     public static Location getLocationByGPS(Context context) {  
  42.         LocationManager mLocationManager = (LocationManager) context  
  43.                 .getSystemService(Context.LOCATION_SERVICE);  
  44.         Location location = mLocationManager  
  45.                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  46.         return location;  
  47.     }  
  48.   
  49.     public static Location getLocationByNetwork(Context context) {  
  50.         LocationManager mLocationManager = (LocationManager) context  
  51.                 .getSystemService(Context.LOCATION_SERVICE);  
  52.         Location location = mLocationManager  
  53.                 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
  54.         return location;  
  55.     }  
  56.   
  57.     public static void openLocationSetting(Activity act, int requestCode) {  
  58.         act.startActivityForResult(new Intent(ACTION_LOCATION_SOURCE_SETTINGS),  
  59.                 requestCode);  
  60.     }  
  61.   
  62.     private static final LocationListener mLocationListener = new LocationListener() {  
  63.   
  64.         public void onLocationChanged(Location location) {  
  65.             // 當坐標改變時觸發此函數,如果Provider傳進相同的坐標,它就不會被觸發   
  66.             System.out.println("onLocationChanged:" + location.getLatitude());  
  67.         }  
  68.   
  69.         public void onProviderDisabled(String provider) {  
  70.             // Provider被disable時觸發此函數,比如GPS被關閉   
  71.             System.out.println("onProviderDisabled:" + provider);  
  72.         }  
  73.   
  74.         public void onProviderEnabled(String provider) {  
  75.             // Provider被enable時觸發此函數,比如GPS被打開   
  76.             System.out.println("onProviderEnabled:" + provider);  
  77.         }  
  78.   
  79.         public void onStatusChanged(String provider, int status, Bundle extras) {  
  80.             // Provider的轉態在可用、暫時不可用和無服務三個狀態直接切換時觸發此函數   
  81.             System.out.println("onStatusChanged:" + status);  
  82.         }  
  83.     };  
  84. }  

需要增加權限:

<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />

Copyright © Linux教程網 All Rights Reserved