优化任务数据计算模块,节省计算量。
This commit is contained in:
		| @@ -1,8 +1,8 @@ | |||||||
| #Created by .winboll/winboll_app_build.gradle | #Created by .winboll/winboll_app_build.gradle | ||||||
| #Mon Oct 27 12:55:27 GMT 2025 | #Tue Oct 28 05:25:30 GMT 2025 | ||||||
| stageCount=15 | stageCount=15 | ||||||
| libraryProject= | libraryProject= | ||||||
| baseVersion=15.0 | baseVersion=15.0 | ||||||
| publishVersion=15.0.14 | publishVersion=15.0.14 | ||||||
| buildCount=2 | buildCount=7 | ||||||
| baseBetaVersion=15.0.15 | baseBetaVersion=15.0.15 | ||||||
|   | |||||||
| @@ -76,7 +76,7 @@ public class MainService extends Service { | |||||||
|     // 数据存储集合(Java 7 基础集合,避免Stream/forEach等Java 8+特性) |     // 数据存储集合(Java 7 基础集合,避免Stream/forEach等Java 8+特性) | ||||||
|     private final ArrayList<PositionModel> mPositionList = new ArrayList<PositionModel>();   // 位置数据列表 |     private final ArrayList<PositionModel> mPositionList = new ArrayList<PositionModel>();   // 位置数据列表 | ||||||
|     private final ArrayList<PositionTaskModel> mAllTasks = new ArrayList<PositionTaskModel>();// 任务数据列表 |     private final ArrayList<PositionTaskModel> mAllTasks = new ArrayList<PositionTaskModel>();// 任务数据列表 | ||||||
|     private PositionModel mCurrentGpsPosition; // 当前GPS定位数据 |     private static PositionModel _mCurrentGpsPosition; // 当前GPS定位数据 | ||||||
|  |  | ||||||
|     // 服务相关变量(Java 7 显式声明,保持原逻辑) |     // 服务相关变量(Java 7 显式声明,保持原逻辑) | ||||||
|     MyServiceConnection mMyServiceConnection; |     MyServiceConnection mMyServiceConnection; | ||||||
| @@ -107,7 +107,7 @@ public class MainService extends Service { | |||||||
| 				public void run() { | 				public void run() { | ||||||
| 					LogUtils.d(TAG, "定时任务触发:开始校验任务(间隔1分钟)"); | 					LogUtils.d(TAG, "定时任务触发:开始校验任务(间隔1分钟)"); | ||||||
| 					// 调用任务校验核心方法(与GPS位置变化时逻辑一致) | 					// 调用任务校验核心方法(与GPS位置变化时逻辑一致) | ||||||
| 					DistanceCalculatorUtil.getInstance(MainService.this).checkAllTaskTriggerCondition(0.0f, 0.0f); | 					DistanceCalculatorUtil.getInstance(MainService.this).checkAllTaskTriggerCondition(MainService._mCurrentGpsPosition); | ||||||
| 				} | 				} | ||||||
| 			}, TASK_CHECK_INIT_DELAY, TASK_CHECK_INTERVAL, TimeUnit.MINUTES); | 			}, TASK_CHECK_INIT_DELAY, TASK_CHECK_INTERVAL, TimeUnit.MINUTES); | ||||||
|  |  | ||||||
| @@ -464,7 +464,7 @@ public class MainService extends Service { | |||||||
|      * @return 当前GPS定位模型(未获取时返回null) |      * @return 当前GPS定位模型(未获取时返回null) | ||||||
|      */ |      */ | ||||||
|     public PositionModel getCurrentGpsPosition() { |     public PositionModel getCurrentGpsPosition() { | ||||||
|         return mCurrentGpsPosition; |         return _mCurrentGpsPosition; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -569,7 +569,7 @@ public class MainService extends Service { | |||||||
|     public void clearAllData() { |     public void clearAllData() { | ||||||
|         mPositionList.clear(); |         mPositionList.clear(); | ||||||
|         mAllTasks.clear(); |         mAllTasks.clear(); | ||||||
|         mCurrentGpsPosition = null; |         _mCurrentGpsPosition = null; | ||||||
|         LogUtils.d(TAG, "clearAllData:已清空所有数据"); |         LogUtils.d(TAG, "clearAllData:已清空所有数据"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -582,7 +582,7 @@ public class MainService extends Service { | |||||||
|             LogUtils.w(TAG, "syncCurrentGpsPosition:位置为空"); |             LogUtils.w(TAG, "syncCurrentGpsPosition:位置为空"); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|         this.mCurrentGpsPosition = position; |         this._mCurrentGpsPosition = position; | ||||||
|         LogUtils.d(TAG, "syncCurrentGpsPosition:成功(纬度=" + position.getLatitude() + ",经度=" + position.getLongitude() + ")"); |         LogUtils.d(TAG, "syncCurrentGpsPosition:成功(纬度=" + position.getLatitude() + ",经度=" + position.getLongitude() + ")"); | ||||||
|         notifyAllGpsListeners(position); |         notifyAllGpsListeners(position); | ||||||
|  |  | ||||||
| @@ -596,14 +596,14 @@ public class MainService extends Service { | |||||||
|      * 同步GPS状态到前台通知(Java 7 匿名Runnable切换主线程,无Lambda) |      * 同步GPS状态到前台通知(Java 7 匿名Runnable切换主线程,无Lambda) | ||||||
|      */ |      */ | ||||||
|     private void syncGpsStatusToNotification() { |     private void syncGpsStatusToNotification() { | ||||||
|         if (!_mIsServiceRunning || mCurrentGpsPosition == null) { |         if (!_mIsServiceRunning || _mCurrentGpsPosition == null) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|         // 格式化通知内容(Java 7 String.format,无String.join等Java 8+方法) |         // 格式化通知内容(Java 7 String.format,无String.join等Java 8+方法) | ||||||
|         final String gpsStatus = String.format( |         final String gpsStatus = String.format( | ||||||
| 			"GPS位置:北纬%.4f° 东经%.4f° | 可见位置:%d个", | 			"GPS位置:北纬%.4f° 东经%.4f° | 可见位置:%d个", | ||||||
| 			mCurrentGpsPosition.getLatitude(), | 			_mCurrentGpsPosition.getLatitude(), | ||||||
| 			mCurrentGpsPosition.getLongitude(), | 			_mCurrentGpsPosition.getLongitude(), | ||||||
| 			mVisiblePositionIds.size() | 			mVisiblePositionIds.size() | ||||||
|         ); |         ); | ||||||
|         // 主线程判断+切换(Java 7 匿名内部类) |         // 主线程判断+切换(Java 7 匿名内部类) | ||||||
| @@ -620,39 +620,6 @@ public class MainService extends Service { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * 强制刷新所有位置距离(GPS更新后调用,计算距离+校验任务触发条件) |  | ||||||
|      */ |  | ||||||
|     public void forceRefreshDistance() { |  | ||||||
|         if (mCurrentGpsPosition == null || mPositionList.isEmpty()) { |  | ||||||
|             LogUtils.w(TAG, "forceRefreshDistance:GPS未获取或位置为空"); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // 遍历所有位置计算距离(Java 7 增强for循环,无Stream) |  | ||||||
|         for (PositionModel pos : mPositionList) { |  | ||||||
|             if (pos.isEnableRealPositionDistance()) { |  | ||||||
|                 try { |  | ||||||
|                     double distance = DistanceCalculatorUtil.calculateHaversineDistance( |  | ||||||
| 						mCurrentGpsPosition.getLatitude(), |  | ||||||
| 						mCurrentGpsPosition.getLongitude(), |  | ||||||
| 						pos.getLatitude(), |  | ||||||
| 						pos.getLongitude() |  | ||||||
|                     ); |  | ||||||
|                     pos.setRealPositionDistance(distance); |  | ||||||
|                 } catch (Exception e) { |  | ||||||
|                     pos.setRealPositionDistance(-1); // 标记距离计算失败 |  | ||||||
|                     LogUtils.e(TAG, "计算距离失败:" + e.getMessage()); |  | ||||||
|                 } |  | ||||||
|             } else { |  | ||||||
|                 pos.setRealPositionDistance(-1); // 未启用距离计算,标记为无效 |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // 距离刷新后校验任务触发条件+通知GPS监听者 |  | ||||||
|         //checkAllTaskTriggerCondition(); |  | ||||||
|         notifyAllGpsListeners(mCurrentGpsPosition); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     // ========================================================================= |     // ========================================================================= | ||||||
| @@ -733,8 +700,7 @@ public class MainService extends Service { | |||||||
|  |  | ||||||
|                     // 同步GPS位置+刷新距离+日志(原逻辑保留) |                     // 同步GPS位置+刷新距离+日志(原逻辑保留) | ||||||
|                     syncCurrentGpsPosition(gpsPos); |                     syncCurrentGpsPosition(gpsPos); | ||||||
|                     forceRefreshDistance(); | 					DistanceCalculatorUtil.getInstance(MainService.this).checkAllTaskTriggerCondition(gpsPos); | ||||||
| 					DistanceCalculatorUtil.getInstance(MainService.this).checkAllTaskTriggerCondition(location.getLatitude(), location.getLongitude()); |  | ||||||
|                     LogUtils.d(TAG, "GPS位置更新:纬度=" + location.getLatitude() + ",经度=" + location.getLongitude()); |                     LogUtils.d(TAG, "GPS位置更新:纬度=" + location.getLatitude() + ",经度=" + location.getLongitude()); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| @@ -780,7 +746,7 @@ public class MainService extends Service { | |||||||
|                 // GPS禁用时清空状态+通知+提示(Java 7 基础逻辑) |                 // GPS禁用时清空状态+通知+提示(Java 7 基础逻辑) | ||||||
|                 if (provider.equals(LocationManager.GPS_PROVIDER)) { |                 if (provider.equals(LocationManager.GPS_PROVIDER)) { | ||||||
|                     isGpsEnabled = false; |                     isGpsEnabled = false; | ||||||
|                     mCurrentGpsPosition = null; |                     _mCurrentGpsPosition = null; | ||||||
|                     String statusDesc = "GPS已关闭(用户手动关闭)"; |                     String statusDesc = "GPS已关闭(用户手动关闭)"; | ||||||
|                     LogUtils.w(TAG, statusDesc); |                     LogUtils.w(TAG, statusDesc); | ||||||
|                     notifyAllGpsStatusListeners(statusDesc); |                     notifyAllGpsStatusListeners(statusDesc); | ||||||
| @@ -968,7 +934,7 @@ public class MainService extends Service { | |||||||
|      * 通知所有GPS监听者位置更新(Java 7 迭代器+弱引用管理,无Stream) |      * 通知所有GPS监听者位置更新(Java 7 迭代器+弱引用管理,无Stream) | ||||||
|      * @param currentGpsPos 当前最新GPS位置 |      * @param currentGpsPos 当前最新GPS位置 | ||||||
|      */ |      */ | ||||||
|     private void notifyAllGpsListeners(PositionModel currentGpsPos) { |     public void notifyAllGpsListeners(PositionModel currentGpsPos) { | ||||||
|         if (currentGpsPos == null || mGpsListeners.isEmpty()) { |         if (currentGpsPos == null || mGpsListeners.isEmpty()) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| @@ -1049,8 +1015,8 @@ public class MainService extends Service { | |||||||
|             mGpsListeners.add(new WeakReference<GpsUpdateListener>(listener)); |             mGpsListeners.add(new WeakReference<GpsUpdateListener>(listener)); | ||||||
|             LogUtils.d(TAG, "GPS监听注册成功,当前数量:" + mGpsListeners.size()); |             LogUtils.d(TAG, "GPS监听注册成功,当前数量:" + mGpsListeners.size()); | ||||||
|             // 注册后立即推送当前GPS位置(避免监听者错过初始数据) |             // 注册后立即推送当前GPS位置(避免监听者错过初始数据) | ||||||
|             if (mCurrentGpsPosition != null) { |             if (_mCurrentGpsPosition != null) { | ||||||
|                 notifySingleListener(listener, mCurrentGpsPosition); |                 notifySingleListener(listener, _mCurrentGpsPosition); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -23,10 +23,9 @@ public class DistanceCalculatorUtil { | |||||||
|     // 1. 私有静态 volatile 实例:保证多线程下实例可见性,避免指令重排序导致的空指针 |     // 1. 私有静态 volatile 实例:保证多线程下实例可见性,避免指令重排序导致的空指针 | ||||||
|     private static volatile DistanceCalculatorUtil sInstance; |     private static volatile DistanceCalculatorUtil sInstance; | ||||||
| 	Context mContext; | 	Context mContext; | ||||||
| 	private ArrayList<PositionModel> mPositionList;   // 位置数据列表 | 	ArrayList<PositionModel> mPositionList;   // 位置数据列表 | ||||||
|     private ArrayList<PositionTaskModel> mAllTasks;// 任务数据列表 |     ArrayList<PositionTaskModel> mAllTasks;// 任务数据列表 | ||||||
| 	double mLatitudeCalculated = 0.0f; | 	PositionModel mGpsPositionCalculated; | ||||||
| 	double mLongitudeCalculated = 0.0f; |  | ||||||
| 	long mLastCalculatedTime = 0; | 	long mLastCalculatedTime = 0; | ||||||
| 	long mMinCalculatedTimeBettween = 30000; // GPS数据更新时,两次计算之间的最小时间间隔 | 	long mMinCalculatedTimeBettween = 30000; // GPS数据更新时,两次计算之间的最小时间间隔 | ||||||
| 	double mMinjumpDistance = 10.0f; // GPS数据更新时,能跳跃距离最小有效值,达到有效值时,两次计算之间的最小时间间隔阀值将被忽略。 | 	double mMinjumpDistance = 10.0f; // GPS数据更新时,能跳跃距离最小有效值,达到有效值时,两次计算之间的最小时间间隔阀值将被忽略。 | ||||||
| @@ -35,9 +34,7 @@ public class DistanceCalculatorUtil { | |||||||
|     private DistanceCalculatorUtil(Context context) { |     private DistanceCalculatorUtil(Context context) { | ||||||
|         // 可选:初始化工具类依赖的资源(如配置参数、缓存等) |         // 可选:初始化工具类依赖的资源(如配置参数、缓存等) | ||||||
| 		mContext = context; | 		mContext = context; | ||||||
| 		MainService mainService = MainService.getInstance(mContext); |  | ||||||
| 		mPositionList = mainService.getPositionList(); |  | ||||||
| 		mAllTasks = mainService.getAllTasks(); |  | ||||||
|         LogUtils.d(TAG, "DistanceCalculatorUtil 单例实例初始化"); |         LogUtils.d(TAG, "DistanceCalculatorUtil 单例实例初始化"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -109,19 +106,22 @@ public class DistanceCalculatorUtil { | |||||||
| 	/** | 	/** | ||||||
|      * 校验所有任务触发条件(距离达标则触发任务通知) |      * 校验所有任务触发条件(距离达标则触发任务通知) | ||||||
|      */ |      */ | ||||||
|     public void checkAllTaskTriggerCondition(double nLatitudeNew, double nLongitudeNew) { |     public void checkAllTaskTriggerCondition(PositionModel currentGpsPosition) { | ||||||
| 		if (mLatitudeCalculated == 0.0f || mLongitudeCalculated == 0.0f) { | 		if (currentGpsPosition == null) { | ||||||
| 			mLatitudeCalculated = nLatitudeNew; | 			LogUtils.d(TAG, "传入坐标参数为空,退出函数。"); | ||||||
| 			mLongitudeCalculated = nLongitudeNew; | 			return; | ||||||
| 			LogUtils.d(TAG, "checkAllTaskTriggerCondition:初始计算位置为空,现在使用新坐标为初始位置。"); |  | ||||||
| 		} | 		} | ||||||
| 		 | 		if (mGpsPositionCalculated == null) { | ||||||
|  | 			mGpsPositionCalculated = currentGpsPosition; | ||||||
|  | 			LogUtils.d(TAG, "最后计算位置记录为空,现在使用新坐标为初始化。"); | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		// 计算频率控制模块 | 		// 计算频率控制模块 | ||||||
| 		// | 		// | ||||||
| 		// 计算与最近一次GPS计算的时间间隔 | 		// 计算与最近一次GPS计算的时间间隔 | ||||||
| 		long nCalculatedTimeBettween = System.currentTimeMillis() - mLastCalculatedTime; | 		long nCalculatedTimeBettween = System.currentTimeMillis() - mLastCalculatedTime; | ||||||
| 		// 计算跳跃距离 | 		// 计算跳跃距离 | ||||||
| 		double jumpDistance = calculateHaversineDistance(mLatitudeCalculated, mLongitudeCalculated, nLatitudeNew, nLongitudeNew); | 		double jumpDistance = calculateHaversineDistance(mGpsPositionCalculated.getLatitude(), mGpsPositionCalculated.getLongitude(), currentGpsPosition.getLatitude(), currentGpsPosition.getLongitude()); | ||||||
| 		if (jumpDistance < mMinjumpDistance) { | 		if (jumpDistance < mMinjumpDistance) { | ||||||
| 			LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:跳跃距离%f,小于50米。", jumpDistance)); | 			LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:跳跃距离%f,小于50米。", jumpDistance)); | ||||||
| 			// 跳跃距离小于最小有效跳跃值 | 			// 跳跃距离小于最小有效跳跃值 | ||||||
| @@ -131,14 +131,22 @@ public class DistanceCalculatorUtil { | |||||||
| 				return; | 				return; | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		 |  | ||||||
|  | 		LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:跳跃距离%f,与上次计算间隔%d,启动任务数据计算。", jumpDistance, nCalculatedTimeBettween)); | ||||||
|  |  | ||||||
|  | 		// 获取位置任务基础数据 | ||||||
|  | 		MainService mainService = MainService.getInstance(mContext); | ||||||
|  | 		mPositionList = mainService.getPositionList(); | ||||||
|  | 		mAllTasks = mainService.getAllTasks(); | ||||||
|  |  | ||||||
| 		// 任务为空,跳过校验。 | 		// 任务为空,跳过校验。 | ||||||
|         if (mPositionList.isEmpty() || mAllTasks.isEmpty()) { |         if (mPositionList.isEmpty() || mAllTasks.isEmpty()) { | ||||||
|             LogUtils.d(TAG, "checkAllTaskTriggerCondition:任务数据为空,跳过校验。"); |             LogUtils.d(TAG, "checkAllTaskTriggerCondition:任务数据为空,跳过校验。"); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| 		 |  | ||||||
| 		LogUtils.d(TAG, String.format("checkAllTaskTriggerCondition:跳跃距离%f,与上次计算间隔%d,启动任务数据计算。", jumpDistance, nCalculatedTimeBettween)); | 		// 更新所有位置点的位置距离数据 | ||||||
|  | 		refreshRealPositionDistance(currentGpsPosition); | ||||||
|  |  | ||||||
|         // 迭代器遍历任务(Java 7 安全遍历,避免并发修改异常) |         // 迭代器遍历任务(Java 7 安全遍历,避免并发修改异常) | ||||||
|         Iterator<PositionTaskModel> taskIter = mAllTasks.iterator(); |         Iterator<PositionTaskModel> taskIter = mAllTasks.iterator(); | ||||||
| @@ -195,10 +203,35 @@ public class DistanceCalculatorUtil { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         MainService.getInstance(mContext).saveAllTasks(); // 持久化更新后的任务状态 |         MainService.getInstance(mContext).saveAllTasks(); // 持久化更新后的任务状态 | ||||||
|  | 		// 记录最后坐标更新点 | ||||||
|  | 		mGpsPositionCalculated = currentGpsPosition; | ||||||
| 		// 记录数据计算时间 | 		// 记录数据计算时间 | ||||||
| 		mLastCalculatedTime = System.currentTimeMillis(); | 		mLastCalculatedTime = System.currentTimeMillis(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 强制刷新所有位置距离(GPS更新后调用,计算距离+校验任务触发条件) | ||||||
|  |      */ | ||||||
|  |     public void refreshRealPositionDistance(PositionModel currentGpsPosition) { | ||||||
|  |         // 遍历所有位置计算距离(Java 7 增强for循环,无Stream) | ||||||
|  |         for (PositionModel pos : mPositionList) { | ||||||
|  |             if (pos.isEnableRealPositionDistance()) { | ||||||
|  | 				double distance = DistanceCalculatorUtil.calculateHaversineDistance( | ||||||
|  | 					currentGpsPosition.getLatitude(), | ||||||
|  | 					currentGpsPosition.getLongitude(), | ||||||
|  | 					pos.getLatitude(), | ||||||
|  | 					pos.getLongitude() | ||||||
|  | 				); | ||||||
|  | 				pos.setRealPositionDistance(distance); | ||||||
|  |             } else { | ||||||
|  |                 pos.setRealPositionDistance(-1); // 未启用距离计算,标记为无效 | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // 距离刷新后通知GPS监听者 | ||||||
|  |         MainService.getInstance(mContext).notifyAllGpsListeners(currentGpsPosition); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen