import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
public class GpsUtil {
private static Context context;
private static LocationManager locationManager;
private static LocationListener locationListener = new MyLocationListener();
/**
*
* Description: <br> 激活gps
* @param locationManager
* @param locationListener
*/
public static void openGps(){
// 每隔1000ms更新一次,並且不考慮位置的變化。最後一個參數是LocationListener的一個引用,我們必須要實現這個類。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0, locationListener);
// locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER,true);
}
/**
*
* Description: <br> 註銷gps
* @param locationManager
* @param locationListener
*/
public static void closeGps(){
if (locationManager != null) {
locationManager.removeUpdates(locationListener);
// locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER,false);
}
}
/**
*
* Description: <br> 根據GPS獲取Location
* @param context
* @param locationManager
* @param locationListener
* @return location
*/
public static Location getLocate(){
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return location;
}
/**
*
* Description: <br>獲取LocationManager
* @param context
* @return
*/
public static void initLocationManager(Context context){
GpsUtil.context = context;
if(locationManager == null){
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
}
/**
*
* Description: <br>衛星個數
* @return
*/
public static int getSatellites() {
int num = locationManager.getGpsStatus(null).getMaxSatellites();
return num;
}
/**判斷GPS是否可用*/
public static boolean isUsed() {
boolean gpsState = Settings.Secure.isLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER);
return gpsState;
}
}