From 59340c0b9107c813d6b3a5c953d693212485ad11 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 20 八月 2025 13:31:22 +0800
Subject: [PATCH] 2025年08月20日13:31:14
---
app/src/main/java/com/hdl/photovoltaic/utils/GPSManagerUtils.java | 25 +++++++++++++++++++++++++
1 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/GPSManagerUtils.java b/app/src/main/java/com/hdl/photovoltaic/utils/GPSManagerUtils.java
index d9b4541..6dfb8cc 100644
--- a/app/src/main/java/com/hdl/photovoltaic/utils/GPSManagerUtils.java
+++ b/app/src/main/java/com/hdl/photovoltaic/utils/GPSManagerUtils.java
@@ -4,15 +4,19 @@
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;
@@ -36,6 +40,27 @@
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<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
+ if (addresses != null && !addresses.isEmpty()) {
+ return addresses.get(0).getCountryCode();
+ }
+ }
+
+ return null;
+ }
+
/**
* 寮�鍚畾浣�
*/
--
Gitblit v1.8.0