Android(安卓)GPS工具 (暂存)
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;
}
}
更多相关文章
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- Android(安卓)手机录音功能实现
- android位置服务GPS经纬度获取
- android之手机震动Vibrate
- android判断用户网络类型
- Android获取、设置音频的音量大小
- android捕获全局异常,CrashHandler
- Android(安卓)Custom
- Android实现复制粘贴功能