package com.hdl.photovoltaic.utils; import android.Manifest; import android.annotation.SuppressLint; import android.content.Context; import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.List; import java.util.Locale; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import com.hdl.photovoltaic.other.HdlLogLogic; /** * @author : Zoro * @date : 2021/10/16 10:04 AM * @desc : */ public class GPSManagerUtils { private GPSManagerUtils() { } static class Singleton { public static GPSManagerUtils INSTANCE = new GPSManagerUtils(); } public static GPSManagerUtils getInstance() { return Singleton.INSTANCE; } public String getCountryCode(Context context) throws IOException { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 处理权限请求 return null; } Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } if (location != null) { Geocoder geocoder = new Geocoder(context, Locale.getDefault()); List
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addresses != null && !addresses.isEmpty()) { return addresses.get(0).getCountryCode(); } } return null; } /** * 开启定位 */ @SuppressLint("MissingPermission") public void getLocation(Context context) { LocationManager locationManager; String locationProvider; //1.获取位置管理器 locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //2.获取位置提供器,GPS或是NetWork List providers = locationManager.getProviders(true); // locationProvider = LocationManager.GPS_PROVIDER; if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS定位 locationProvider = LocationManager.GPS_PROVIDER; } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是网络定位 locationProvider = LocationManager.NETWORK_PROVIDER; } else { return; } //高版本的权限检查 if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } //3.获取上次的位置,一般第一次运行,此值为null @SuppressLint("MissingPermission") Location location = locationManager.getLastKnownLocation(locationProvider); if (location != null) { showLocation(location); } else { // 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace //60秒更省电 locationManager.requestLocationUpdates(locationProvider, 30 * 1000, 10, mListener); } } /** * 获取定位经纬度 * * @param location */ private void showLocation(Location location) { if (location != null) {//小数点后面6位 BigDecimal longitude = BigDecimal.valueOf(location.getLongitude()); BigDecimal latitude = BigDecimal.valueOf(location.getLatitude()); String lon = longitude.setScale(6, RoundingMode.HALF_DOWN).doubleValue() + ""; String lat = latitude.setScale(6, RoundingMode.HALF_DOWN).doubleValue() + ""; HdlLogLogic.print("纬度--->" + lon, false); HdlLogLogic.print("经度--->" + lat, false); // if(!TextUtils.isEmpty(lon)&&!TextUtils.isEmpty(lat)){ // DataController.getInstance().lonLatUpdate(lon,lat,new IResponseCallBack() { // @Override // public void onSuccess(String objects) { // } // // @Override // public void onFailure(HttpException error) { // } // }); // } } } private LocationListener mListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } // 如果位置发生变化,重新显示 @Override public void onLocationChanged(@NonNull Location location) { showLocation(location); } }; }