|
|
@@ -24,11 +24,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
public class AMapHelper {
|
|
|
private static final String TAG = "AMapHelper";
|
|
|
- public static final String KEY_LAST_TRACK_ID = "amap_last_track_id";
|
|
|
private static final int AMAP_NOTIFICATION_ID = 350;
|
|
|
private static final String AMAP_NOTIFICATION_CHANNEL_ID = "amap_notification_channel_id";
|
|
|
private static final String AMAP_NOTIFICATION_CHANNEL_NAME = "Location Tracking";
|
|
|
- private static final AtomicBoolean isLocationInit = new AtomicBoolean(false);
|
|
|
private static final ArrayList<OnLocationListener> locationListeners = new ArrayList<>();
|
|
|
private static AMapLocation lastLocation;
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
@@ -38,48 +36,36 @@ public class AMapHelper {
|
|
|
Manifest.permission.ACCESS_COARSE_LOCATION
|
|
|
};
|
|
|
|
|
|
- public static void init(Context context) {
|
|
|
- if (ProcessUtil.isMainProcess(context)) {
|
|
|
- initLocation(context);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static void initLocation(Context context) {
|
|
|
- if (isLocationInit.compareAndSet(false, true)) {
|
|
|
- try {
|
|
|
- LogUtil.d(TAG, "amap ... initLocation...");
|
|
|
- AMapLocationClient.updatePrivacyShow(context, true, true);
|
|
|
- AMapLocationClient.updatePrivacyAgree(context, true);
|
|
|
- aMapLocationClient = new AMapLocationClient(context);
|
|
|
- aMapLocationClient.setLocationOption(new AMapLocationClientOption().setInterval(5000));
|
|
|
- aMapLocationClient.setLocationListener(aMapLocation -> {
|
|
|
- if (aMapLocation != null) {
|
|
|
- if (aMapLocation.getErrorCode() == 0) {
|
|
|
- lastLocation = aMapLocation;
|
|
|
- LogUtil.d(TAG, "onLocationChanged: " + aMapLocation);
|
|
|
- for (OnLocationListener locationListener : locationListeners) {
|
|
|
- locationListener.onLocationChanged(aMapLocation);
|
|
|
- }
|
|
|
- }
|
|
|
+ public static void init(Context context) throws Exception {
|
|
|
+ LogUtil.d(TAG, "amap ... initLocation...");
|
|
|
+ AMapLocationClient.updatePrivacyShow(context, true, true);
|
|
|
+ AMapLocationClient.updatePrivacyAgree(context, true);
|
|
|
+ aMapLocationClient = new AMapLocationClient(context);
|
|
|
+ aMapLocationClient.setLocationOption(new AMapLocationClientOption().setInterval(5000));
|
|
|
+ aMapLocationClient.setLocationListener(aMapLocation -> {
|
|
|
+ if (aMapLocation != null) {
|
|
|
+ if (aMapLocation.getErrorCode() == 0) {
|
|
|
+ lastLocation = aMapLocation;
|
|
|
+ LogUtil.d(TAG, "onLocationChanged: " + aMapLocation);
|
|
|
+ for (OnLocationListener locationListener : locationListeners) {
|
|
|
+ locationListener.onLocationChanged(aMapLocation);
|
|
|
}
|
|
|
- });
|
|
|
- if (PermissionUtil.hasPermission(context, LOCATION_PERMISSION)) {
|
|
|
- aMapLocationClient.startLocation();
|
|
|
- aMapLocationClient.enableBackgroundLocation(AMAP_NOTIFICATION_ID, createLocationNotification(context));
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- isLocationInit.set(false);
|
|
|
- LogUtil.e(TAG, "initLocation failed.", e);
|
|
|
}
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public static void startLocation(Context context) {
|
|
|
- if (isLocationInit.get()) {
|
|
|
+ if (aMapLocationClient == null) {
|
|
|
+ throw new IllegalStateException("AMapLocationClient is not initialized. Call init() first.");
|
|
|
+ }
|
|
|
+ if (PermissionUtil.hasPermission(context, LOCATION_PERMISSION)) {
|
|
|
+ LogUtil.d(TAG, "startLocation...");
|
|
|
aMapLocationClient.startLocation();
|
|
|
aMapLocationClient.enableBackgroundLocation(AMAP_NOTIFICATION_ID, createLocationNotification(context));
|
|
|
- } else {
|
|
|
- initLocation(context);
|
|
|
+ }else{
|
|
|
+ LogUtil.w(TAG, "startLocation: No location permission granted.");
|
|
|
+ throw new SecurityException("Location permissions are required to start location tracking.");
|
|
|
}
|
|
|
}
|
|
|
|