添加GPS数据处理工具类,整合分散的GPS处理方法

- 新增 GpsUtil 工具类,提供 parseFromIntent、parseFromCursor、
  formatTime、formatLatLng、formatOther、speedToKmh、formatSpeed 方法
- GpsReceiveService 调用 GpsUtil.parseFromIntent() 替代内联解析
- GpsAdapter 调用 GpsUtil 格式化方法替代内联格式化,移除 SimpleDateFormat 字段
- GpsDbManager 调用 GpsUtil.parseFromCursor() 消除两处重复的游标映射代码
This commit is contained in:
MiMo
2026-07-06 20:02:13 +08:00
parent afd57c8d70
commit 32afb3ad74
5 changed files with 129 additions and 46 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Mon Jul 06 13:56:29 CST 2026
#Mon Jul 06 19:59:58 CST 2026
stageCount=2
libraryProject=
baseVersion=15.20
publishVersion=15.20.1
buildCount=9
buildCount=12
baseBetaVersion=15.20.2
@@ -9,8 +9,7 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cc.winboll.studio.bytheway.R;
import cc.winboll.studio.bytheway.models.GpsBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import cc.winboll.studio.bytheway.utils.GpsUtil;
import java.util.List;
/**
@@ -21,7 +20,6 @@ public class GpsAdapter extends RecyclerView.Adapter<GpsAdapter.GpsHolder> {
private final Context context;
private final List<GpsBean> gpsList;
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public GpsAdapter(Context context, List<GpsBean> gpsList) {
this.context = context;
@@ -39,15 +37,12 @@ public class GpsAdapter extends RecyclerView.Adapter<GpsAdapter.GpsHolder> {
public void onBindViewHolder(@NonNull GpsHolder holder, int position) {
GpsBean bean = gpsList.get(position);
//时间格式化
String time = sdf.format(new Date(bean.getReceiveTime()));
String time = GpsUtil.formatTime(bean.getReceiveTime());
holder.tvTime.setText("接收时间:" + time);
//经纬度
holder.tvLatLng.setText("纬度:" + bean.getLatitude() + " 经度:" + bean.getLongitude());
holder.tvLatLng.setText(GpsUtil.formatLatLng(bean.getLatitude(), bean.getLongitude()));
//海拔、精度、速度
holder.tvOther.setText("精度:"+bean.getAccuracy()+"m 海拔:"+bean.getAltitude()+"m 速度:"+bean.getSpeed()+"m/s");
holder.tvOther.setText(GpsUtil.formatOther(bean.getAccuracy(), bean.getAltitude(), bean.getSpeed()));
}
@Override
@@ -5,6 +5,7 @@ import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cc.winboll.studio.bytheway.models.GpsBean;
import cc.winboll.studio.bytheway.utils.GpsUtil;
import cc.winboll.studio.libappbase.LogUtils;
import java.util.ArrayList;
import java.util.List;
@@ -51,16 +52,7 @@ public class GpsDbManager {
if (cursor.moveToFirst()) {
do {
final GpsBean bean = new GpsBean();
bean.setId(cursor.getInt(0));
bean.setLatitude(cursor.getDouble(1));
bean.setLongitude(cursor.getDouble(2));
bean.setAccuracy(cursor.getFloat(3));
bean.setAltitude(cursor.getDouble(4));
bean.setSpeed(cursor.getFloat(5));
bean.setReceiveTime(cursor.getLong(6));
list.add(bean);
list.add(GpsUtil.parseFromCursor(cursor));
} while (cursor.moveToNext());
}
@@ -82,16 +74,7 @@ public class GpsDbManager {
if (cursor.moveToFirst()) {
do {
final GpsBean bean = new GpsBean();
bean.setId(cursor.getInt(0));
bean.setLatitude(cursor.getDouble(1));
bean.setLongitude(cursor.getDouble(2));
bean.setAccuracy(cursor.getFloat(3));
bean.setAltitude(cursor.getDouble(4));
bean.setSpeed(cursor.getFloat(5));
bean.setReceiveTime(cursor.getLong(6));
list.add(bean);
list.add(GpsUtil.parseFromCursor(cursor));
} while (cursor.moveToNext());
}
@@ -13,6 +13,7 @@ import android.os.IBinder;
import android.widget.Toast;
import cc.winboll.studio.bytheway.helpers.GpsDbManager;
import cc.winboll.studio.bytheway.models.GpsBean;
import cc.winboll.studio.bytheway.utils.GpsUtil;
import cc.winboll.studio.libappbase.LogUtils;
/**
@@ -114,21 +115,7 @@ public class GpsReceiveService extends Service {
* 解析广播GPS数据并写入SQLite数据库
*/
private void parseAndSaveGpsData(final Intent intent) {
final double latitude = intent.getDoubleExtra("latitude", 0);
final double longitude = intent.getDoubleExtra("longitude", 0);
final float accuracy = intent.getFloatExtra("accuracy", 0);
final double altitude = intent.getDoubleExtra("altitude", 0);
final float speed = intent.getFloatExtra("speed", 0);
final long time = intent.getLongExtra("receive_time", System.currentTimeMillis());
final GpsBean bean = new GpsBean();
bean.setLatitude(latitude);
bean.setLongitude(longitude);
bean.setAccuracy(accuracy);
bean.setAltitude(altitude);
bean.setSpeed(speed);
bean.setReceiveTime(time);
final GpsBean bean = GpsUtil.parseFromIntent(intent);
dbManager.insertGps(bean);
LogUtils.d("GpsReceiveService", "收到GPS广播,已保存定位数据");
}
@@ -0,0 +1,118 @@
package cc.winboll.studio.bytheway.utils;
import android.content.Intent;
import android.database.Cursor;
import cc.winboll.studio.bytheway.models.GpsBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* GPS数据处理工具类
* 集合项目中所有GPS数据处理方法,供外部统一调用
* @author ZhanGSKen<zhangsken@qq.com>
*/
public class GpsUtil {
// Intent广播中的GPS数据键名
public static final String KEY_LATITUDE = "latitude";
public static final String KEY_LONGITUDE = "longitude";
public static final String KEY_ACCURACY = "accuracy";
public static final String KEY_ALTITUDE = "altitude";
public static final String KEY_SPEED = "speed";
public static final String KEY_RECEIVE_TIME = "receive_time";
// 时间格式
private static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";
/**
* 从Intent广播数据中解析GPS定位信息,构造GpsBean对象
* @param intent GPS定位广播Intent
* @return 解析后的GpsBean对象
*/
public static GpsBean parseFromIntent(final Intent intent) {
final double latitude = intent.getDoubleExtra(KEY_LATITUDE, 0);
final double longitude = intent.getDoubleExtra(KEY_LONGITUDE, 0);
final float accuracy = intent.getFloatExtra(KEY_ACCURACY, 0);
final double altitude = intent.getDoubleExtra(KEY_ALTITUDE, 0);
final float speed = intent.getFloatExtra(KEY_SPEED, 0);
final long time = intent.getLongExtra(KEY_RECEIVE_TIME, System.currentTimeMillis());
final GpsBean bean = new GpsBean();
bean.setLatitude(latitude);
bean.setLongitude(longitude);
bean.setAccuracy(accuracy);
bean.setAltitude(altitude);
bean.setSpeed(speed);
bean.setReceiveTime(time);
return bean;
}
/**
* 从数据库游标中解析GPS定位信息,构造GpsBean对象
* 游标字段顺序:id, latitude, longitude, accuracy, altitude, speed, receive_time
* @param cursor 数据库查询游标(需已定位到目标行)
* @return 解析后的GpsBean对象
*/
public static GpsBean parseFromCursor(final Cursor cursor) {
final GpsBean bean = new GpsBean();
bean.setId(cursor.getInt(0));
bean.setLatitude(cursor.getDouble(1));
bean.setLongitude(cursor.getDouble(2));
bean.setAccuracy(cursor.getFloat(3));
bean.setAltitude(cursor.getDouble(4));
bean.setSpeed(cursor.getFloat(5));
bean.setReceiveTime(cursor.getLong(6));
return bean;
}
/**
* 将时间戳格式化为可读时间字符串
* 格式:yyyy-MM-dd HH:mm:ss
* @param timestamp 毫秒时间戳
* @return 格式化后的时间字符串
*/
public static String formatTime(final long timestamp) {
final SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_TIME, Locale.getDefault());
return sdf.format(new Date(timestamp));
}
/**
* 格式化经纬度显示文本
* @param latitude 纬度
* @param longitude 经度
* @return 格式化后的经纬度字符串,如"纬度:39.9 经度:116.4"
*/
public static String formatLatLng(final double latitude, final double longitude) {
return "纬度:" + latitude + " 经度:" + longitude;
}
/**
* 格式化精度、海拔、速度显示文本
* @param accuracy 精度(米)
* @param altitude 海拔(米)
* @param speed 速度(m/s
* @return 格式化后的字符串,如"精度:10.0m 海拔:100.0m 速度:5.0m/s"
*/
public static String formatOther(final float accuracy, final double altitude, final float speed) {
return "精度:" + accuracy + "m 海拔:" + altitude + "m 速度:" + speed + "m/s";
}
/**
* 将速度从m/s转换为km/h
* @param speedMps 速度(m/s
* @return 速度(km/h
*/
public static float speedToKmh(final float speedMps) {
return speedMps * 3.6f;
}
/**
* 格式化速度显示文本(km/h)
* @param speedMps 速度(m/s
* @return 格式化后的速度字符串,如"18.0km/h"
*/
public static String formatSpeed(final float speedMps) {
return speedToKmh(speedMps) + "km/h";
}
}