修复实时距离显示问题
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -22,7 +22,7 @@ public class PositionModel extends BaseBean {
|
||||
double latitude;
|
||||
// 位置备注(空值时显示“无备注”)
|
||||
String memo;
|
||||
// 定位点与指定点实时距离大小
|
||||
// 定位点与指定点实时距离长度
|
||||
double realPositionDistance;
|
||||
// 是否启用实时距离计算
|
||||
boolean isEnableRealPositionDistance;
|
||||
|
||||
@@ -8,6 +8,7 @@ package cc.winboll.studio.positions.services;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.Binder;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Looper;
|
||||
@@ -38,21 +39,20 @@ public class DistanceRefreshService extends Service {
|
||||
public static final long REFRESH_INTERVAL = 5000; // 5秒刷新一次
|
||||
public static final int MSG_UPDATE_DISTANCE = 1001;
|
||||
public static final String KEY_POSITION_ID = "key_position_id";
|
||||
public static final String KEY_DISTANCE_TEXT = "key_distance_text";
|
||||
public static final String KEY_DISTANCE_COLOR = "key_distance_color";
|
||||
|
||||
// 核心成员变量(Java7:明确泛型初始化)
|
||||
private Timer mDistanceTimer;
|
||||
private Handler mMainHandler;
|
||||
private PositionModel mCurrentGpsPosition;
|
||||
private ArrayList<PositionModel> mPositionList;
|
||||
private ArrayList<PositionModel> mPositionList; // 持有Adapter传递的原列表引用
|
||||
private ArrayList<PositionTaskModel> mAllPositionTasks;
|
||||
private Map<String, Integer> mVisibleDistanceViewTags = new HashMap<String, Integer>();
|
||||
private OnDistanceUpdateReceiver mUpdateReceiver;
|
||||
private boolean isPositionListSynced = false; // 新增:标记位置列表是否已同步
|
||||
|
||||
// 数据同步与消息接收接口(Activity/Adapter实现)
|
||||
public interface OnDistanceUpdateReceiver {
|
||||
void onDistanceUpdate(String positionId, String distanceText, int distanceColor);
|
||||
void onDistanceUpdate(String positionId); // 简化:仅传递位置ID
|
||||
int getColorRes(int resId);
|
||||
}
|
||||
|
||||
@@ -75,13 +75,13 @@ public class DistanceRefreshService extends Service {
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (msg.what == MSG_UPDATE_DISTANCE && mUpdateReceiver != null) {
|
||||
// 解析消息(Java7:显式调用getData(),避免链式调用)
|
||||
String positionId = msg.getData().getString(KEY_POSITION_ID);
|
||||
String distanceText = msg.getData().getString(KEY_DISTANCE_TEXT);
|
||||
int distanceColor = msg.getData().getInt(KEY_DISTANCE_COLOR);
|
||||
LogUtils.d(TAG, "接收消息→转发更新:位置ID=" + positionId + ",距离文本=" + distanceText);
|
||||
|
||||
mUpdateReceiver.onDistanceUpdate(positionId, distanceText, distanceColor);
|
||||
// 解析消息:仅获取位置ID
|
||||
Bundle data = msg.getData();
|
||||
String positionId = data.getString(KEY_POSITION_ID);
|
||||
if (positionId != null) {
|
||||
LogUtils.d(TAG, "接收消息→转发更新:位置ID=" + positionId);
|
||||
mUpdateReceiver.onDistanceUpdate(positionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -117,7 +117,7 @@ public class DistanceRefreshService extends Service {
|
||||
mDistanceTimer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
LogUtils.d(TAG, "定时器触发→开始计算距离,可见位置数量=" + mVisibleDistanceViewTags.size());
|
||||
LogUtils.d(TAG, "定时器触发→开始计算距离,位置列表同步状态=" + isPositionListSynced);
|
||||
calculateAndSendDistanceUpdates();
|
||||
checkAndTriggerTasks();
|
||||
}
|
||||
@@ -125,75 +125,47 @@ public class DistanceRefreshService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算距离并发送UI更新消息
|
||||
* Java7:使用迭代器遍历Map(替代forEach Lambda),显式空判断
|
||||
* 核心修改:计算距离并更新到mPositionList,仅发送位置ID通知
|
||||
* Java7:使用迭代器遍历,显式空判断
|
||||
*/
|
||||
private void calculateAndSendDistanceUpdates() {
|
||||
// 无可见控件/数据/接收器时,直接返回
|
||||
if (mVisibleDistanceViewTags.isEmpty()) {
|
||||
LogUtils.d(TAG, "无需要更新的可见控件,跳过计算");
|
||||
// 前置校验:位置列表未同步/为空/无GPS,直接返回
|
||||
if (!isPositionListSynced || mPositionList.isEmpty()) {
|
||||
LogUtils.d(TAG, "位置列表未同步/为空,跳过距离计算");
|
||||
return;
|
||||
}
|
||||
if (mPositionList.isEmpty()) {
|
||||
LogUtils.d(TAG, "位置列表为空,无法计算距离");
|
||||
return;
|
||||
}
|
||||
if (mUpdateReceiver == null) {
|
||||
LogUtils.d(TAG, "未设置消息接收器(Adapter未绑定),无法发送更新");
|
||||
if (mCurrentGpsPosition == null) {
|
||||
LogUtils.d(TAG, "无当前GPS位置,跳过距离计算");
|
||||
return;
|
||||
}
|
||||
|
||||
// Java7:使用Iterator遍历Map.Entry(替代forEach Lambda)
|
||||
Iterator<Map.Entry<String, Integer>> entryIterator = mVisibleDistanceViewTags.entrySet().iterator();
|
||||
while (entryIterator.hasNext()) {
|
||||
Map.Entry<String, Integer> entry = entryIterator.next();
|
||||
String positionId = entry.getKey();
|
||||
PositionModel targetModel = findPositionModelById(positionId);
|
||||
// 遍历所有位置项,计算并设置realPositionDistance
|
||||
Iterator<PositionModel> positionIter = mPositionList.iterator();
|
||||
while (positionIter.hasNext()) {
|
||||
PositionModel targetModel = positionIter.next();
|
||||
String positionId = targetModel.getPositionId();
|
||||
|
||||
if (targetModel == null) {
|
||||
// 位置模型不存在时,发送“无效位置”消息
|
||||
sendDistanceUpdateMessage(
|
||||
positionId,
|
||||
"实时距离:位置无效",
|
||||
mUpdateReceiver.getColorRes(R.color.colorRed)
|
||||
);
|
||||
LogUtils.d(TAG, "位置ID=" + positionId + " 未找到对应模型,发送无效提示");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 距离计算与文本生成
|
||||
String distanceText;
|
||||
int distanceColor;
|
||||
if (!targetModel.isEnableRealPositionDistance()) {
|
||||
distanceText = "实时距离:未启用";
|
||||
distanceColor = mUpdateReceiver.getColorRes(R.color.colorGrayText);
|
||||
} else if (mCurrentGpsPosition == null) {
|
||||
// 无GPS时持续发送“等待定位”(避免默认文本回落)
|
||||
distanceText = "实时距离:等待GPS定位";
|
||||
distanceColor = mUpdateReceiver.getColorRes(R.color.colorGrayText);
|
||||
LogUtils.d(TAG, "位置ID=" + positionId + " 无GPS数据,发送等待提示");
|
||||
} else {
|
||||
if (targetModel.isEnableRealPositionDistance()) {
|
||||
// 状态为true:计算距离(米),设置到realPositionDistance
|
||||
try {
|
||||
// 计算距离(复用PositionModel静态方法)
|
||||
double distanceM = PositionModel.calculatePositionDistance(mCurrentGpsPosition, targetModel, false);
|
||||
judgeTaskBingoStatus(targetModel, (int) distanceM);
|
||||
// 格式化距离文本(Java7:显式类型转换,避免自动拆箱问题)
|
||||
if (distanceM < 1000) {
|
||||
distanceText = String.format("实时距离:%.1f 米", distanceM);
|
||||
} else {
|
||||
distanceText = String.format("实时距离:%.1f 千米", distanceM / 1000);
|
||||
}
|
||||
distanceColor = mUpdateReceiver.getColorRes(R.color.colorEnableGreen);
|
||||
LogUtils.d(TAG, "位置ID=" + positionId + " 计算完成:" + distanceText);
|
||||
double distanceM = PositionModel.calculatePositionDistance(
|
||||
mCurrentGpsPosition, targetModel, false
|
||||
);
|
||||
targetModel.setRealPositionDistance(distanceM); // 存储计算结果到列表项
|
||||
LogUtils.d(TAG, "位置ID=" + positionId + " 计算距离:" + distanceM + "米");
|
||||
} catch (IllegalArgumentException e) {
|
||||
distanceText = "实时距离:数据无效";
|
||||
distanceColor = mUpdateReceiver.getColorRes(R.color.colorRed);
|
||||
LogUtils.e(TAG, "位置ID=" + positionId + " 计算异常:" + e.getMessage());
|
||||
// 计算异常时,设置为-1(标记无效)
|
||||
targetModel.setRealPositionDistance(-1);
|
||||
LogUtils.e(TAG, "位置ID=" + positionId + " 距离计算异常:" + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
// 状态为false:强制设置realPositionDistance为-1
|
||||
targetModel.setRealPositionDistance(-1);
|
||||
LogUtils.d(TAG, "位置ID=" + positionId + " 未启用距离计算,设置距离为-1");
|
||||
}
|
||||
|
||||
// 发送消息到UI线程
|
||||
sendDistanceUpdateMessage(positionId, distanceText, distanceColor);
|
||||
// 发送更新通知(仅传位置ID)
|
||||
sendDistanceUpdateMessage(positionId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +174,7 @@ public class DistanceRefreshService extends Service {
|
||||
* Java7:使用迭代器遍历任务列表(替代forEach Lambda)
|
||||
*/
|
||||
private void checkAndTriggerTasks() {
|
||||
if (mAllPositionTasks.isEmpty()) {
|
||||
if (mAllPositionTasks.isEmpty() || !isPositionListSynced || mPositionList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// Java7:Iterator遍历ArrayList(替代forEach Lambda)
|
||||
@@ -216,64 +188,69 @@ public class DistanceRefreshService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断任务触发状态(仅修改数据,UI更新由Adapter处理)
|
||||
* 新增:判断任务触发状态(基于mPositionList中的realPositionDistance)
|
||||
* Java7:迭代器遍历任务列表,显式状态判断
|
||||
*/
|
||||
private void judgeTaskBingoStatus(PositionModel model, int distanceM) {
|
||||
String targetPosId = model.getPositionId();
|
||||
// Java7:Iterator遍历任务列表
|
||||
Iterator<PositionTaskModel> taskIterator = mAllPositionTasks.iterator();
|
||||
while (taskIterator.hasNext()) {
|
||||
PositionTaskModel task = taskIterator.next();
|
||||
if (targetPosId.equals(task.getPositionId())) {
|
||||
boolean oldBingoState = task.isBingo();
|
||||
boolean newBingoState = false;
|
||||
private void judgeTaskBingoStatus() {
|
||||
if (!isPositionListSynced || mPositionList.isEmpty() || mAllPositionTasks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 根据任务条件判断新状态(Java7:if-else替代三元表达式,增强可读性)
|
||||
if (task.isGreaterThan()) {
|
||||
newBingoState = task.isEnable() && distanceM > task.getDiscussDistance();
|
||||
} else if (task.isLessThan()) {
|
||||
newBingoState = task.isEnable() && distanceM < task.getDiscussDistance();
|
||||
} else {
|
||||
newBingoState = task.isEnable();
|
||||
}
|
||||
Iterator<PositionModel> posIter = mPositionList.iterator();
|
||||
while (posIter.hasNext()) {
|
||||
PositionModel posModel = posIter.next();
|
||||
String posId = posModel.getPositionId();
|
||||
double distanceM = posModel.getRealPositionDistance();
|
||||
|
||||
// 更新任务状态(仅状态变化时更新)
|
||||
if (newBingoState != oldBingoState) {
|
||||
task.setIsBingo(newBingoState);
|
||||
// 遍历绑定当前位置的任务
|
||||
Iterator<PositionTaskModel> taskIter = mAllPositionTasks.iterator();
|
||||
while (taskIter.hasNext()) {
|
||||
PositionTaskModel task = taskIter.next();
|
||||
if (posId.equals(task.getPositionId()) && distanceM != -1) {
|
||||
boolean oldBingoState = task.isBingo();
|
||||
boolean newBingoState = false;
|
||||
|
||||
// 根据任务条件判断新状态
|
||||
if (task.isGreaterThan()) {
|
||||
newBingoState = task.isEnable() && distanceM > task.getDiscussDistance();
|
||||
} else if (task.isLessThan()) {
|
||||
newBingoState = task.isEnable() && distanceM < task.getDiscussDistance();
|
||||
}
|
||||
|
||||
// 仅状态变化时更新
|
||||
if (newBingoState != oldBingoState) {
|
||||
task.setIsBingo(newBingoState);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送距离更新消息(通过Handler发送到UI线程)
|
||||
* 简化:仅发送位置ID通知(Adapter从mPositionList读取数据)
|
||||
* Java7:显式创建Message,避免obtainMessage链式调用
|
||||
*/
|
||||
private void sendDistanceUpdateMessage(String positionId, String distanceText, int distanceColor) {
|
||||
private void sendDistanceUpdateMessage(String positionId) {
|
||||
Message msg = mMainHandler.obtainMessage(MSG_UPDATE_DISTANCE);
|
||||
// Java7:显式调用put方法(替代链式put)
|
||||
msg.getData().putString(KEY_POSITION_ID, positionId);
|
||||
msg.getData().putString(KEY_DISTANCE_TEXT, distanceText);
|
||||
msg.getData().putInt(KEY_DISTANCE_COLOR, distanceColor);
|
||||
Bundle data = new Bundle();
|
||||
data.putString(KEY_POSITION_ID, positionId);
|
||||
msg.setData(data);
|
||||
mMainHandler.sendMessage(msg);
|
||||
LogUtils.d(TAG, "发送消息→位置ID=" + positionId + ",距离文本=" + distanceText);
|
||||
LogUtils.d(TAG, "发送更新通知:位置ID=" + positionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据位置ID查找位置模型(工具方法)
|
||||
* Java7:迭代器遍历位置列表(替代stream().filter())
|
||||
* 核心修改:同步位置列表(持有原引用,不拷贝)
|
||||
*/
|
||||
private PositionModel findPositionModelById(String positionId) {
|
||||
// Java7:Iterator遍历ArrayList
|
||||
Iterator<PositionModel> modelIterator = mPositionList.iterator();
|
||||
while (modelIterator.hasNext()) {
|
||||
PositionModel model = modelIterator.next();
|
||||
if (positionId.equals(model.getPositionId())) {
|
||||
return model;
|
||||
}
|
||||
public void syncPositionList(ArrayList<PositionModel> positionList) {
|
||||
if (positionList != null) {
|
||||
this.mPositionList = positionList; // 持有外部列表引用
|
||||
this.isPositionListSynced = true;
|
||||
LogUtils.d(TAG, "同步位置列表(持有引用):数量=" + positionList.size());
|
||||
} else {
|
||||
this.isPositionListSynced = false;
|
||||
LogUtils.w(TAG, "同步位置列表失败:传入列表为null");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------------------- 对外API(供Activity/Adapter调用) ----------------------
|
||||
@@ -286,14 +263,6 @@ public class DistanceRefreshService extends Service {
|
||||
LogUtils.d(TAG, "同步GPS位置:纬度=" + (currentGpsPosition != null ? currentGpsPosition.getLatitude() : 0.0f));
|
||||
}
|
||||
|
||||
public void syncPositionList(ArrayList<PositionModel> positionList) {
|
||||
if (positionList != null) {
|
||||
this.mPositionList.clear();
|
||||
this.mPositionList.addAll(positionList);
|
||||
LogUtils.d(TAG, "同步位置列表:数量=" + positionList.size());
|
||||
}
|
||||
}
|
||||
|
||||
public void syncAllPositionTasks(ArrayList<PositionTaskModel> allPositionTasks) {
|
||||
if (allPositionTasks != null) {
|
||||
this.mAllPositionTasks.clear();
|
||||
@@ -331,10 +300,11 @@ public class DistanceRefreshService extends Service {
|
||||
}
|
||||
// 清空数据,解除引用(避免内存泄漏)
|
||||
mCurrentGpsPosition = null;
|
||||
mPositionList.clear();
|
||||
mPositionList = null; // 释放列表引用
|
||||
mAllPositionTasks.clear();
|
||||
mVisibleDistanceViewTags.clear();
|
||||
mUpdateReceiver = null;
|
||||
isPositionListSynced = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user