From 32afb3ad74b35e3c84fd9f272f44168190e2b507 Mon Sep 17 00:00:00 2001 From: MiMo Date: Mon, 6 Jul 2026 20:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0GPS=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E5=90=88=E5=88=86=E6=95=A3=E7=9A=84GPS=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 GpsUtil 工具类,提供 parseFromIntent、parseFromCursor、 formatTime、formatLatLng、formatOther、speedToKmh、formatSpeed 方法 - GpsReceiveService 调用 GpsUtil.parseFromIntent() 替代内联解析 - GpsAdapter 调用 GpsUtil 格式化方法替代内联格式化,移除 SimpleDateFormat 字段 - GpsDbManager 调用 GpsUtil.parseFromCursor() 消除两处重复的游标映射代码 --- bytheway/build.properties | 4 +- .../studio/bytheway/adapters/GpsAdapter.java | 13 +- .../studio/bytheway/helpers/GpsDbManager.java | 23 +--- .../bytheway/services/GpsReceiveService.java | 17 +-- .../studio/bytheway/utils/GpsUtil.java | 118 ++++++++++++++++++ 5 files changed, 129 insertions(+), 46 deletions(-) create mode 100644 bytheway/src/main/java/cc/winboll/studio/bytheway/utils/GpsUtil.java diff --git a/bytheway/build.properties b/bytheway/build.properties index 62b154d..03c9c15 100644 --- a/bytheway/build.properties +++ b/bytheway/build.properties @@ -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 diff --git a/bytheway/src/main/java/cc/winboll/studio/bytheway/adapters/GpsAdapter.java b/bytheway/src/main/java/cc/winboll/studio/bytheway/adapters/GpsAdapter.java index e733d15..f3b2be9 100644 --- a/bytheway/src/main/java/cc/winboll/studio/bytheway/adapters/GpsAdapter.java +++ b/bytheway/src/main/java/cc/winboll/studio/bytheway/adapters/GpsAdapter.java @@ -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 { private final Context context; private final List gpsList; - private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public GpsAdapter(Context context, List gpsList) { this.context = context; @@ -39,15 +37,12 @@ public class GpsAdapter extends RecyclerView.Adapter { 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 diff --git a/bytheway/src/main/java/cc/winboll/studio/bytheway/helpers/GpsDbManager.java b/bytheway/src/main/java/cc/winboll/studio/bytheway/helpers/GpsDbManager.java index 6827d06..d114771 100644 --- a/bytheway/src/main/java/cc/winboll/studio/bytheway/helpers/GpsDbManager.java +++ b/bytheway/src/main/java/cc/winboll/studio/bytheway/helpers/GpsDbManager.java @@ -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()); } diff --git a/bytheway/src/main/java/cc/winboll/studio/bytheway/services/GpsReceiveService.java b/bytheway/src/main/java/cc/winboll/studio/bytheway/services/GpsReceiveService.java index 060ad9c..633d287 100644 --- a/bytheway/src/main/java/cc/winboll/studio/bytheway/services/GpsReceiveService.java +++ b/bytheway/src/main/java/cc/winboll/studio/bytheway/services/GpsReceiveService.java @@ -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广播,已保存定位数据"); } diff --git a/bytheway/src/main/java/cc/winboll/studio/bytheway/utils/GpsUtil.java b/bytheway/src/main/java/cc/winboll/studio/bytheway/utils/GpsUtil.java new file mode 100644 index 0000000..4417ce5 --- /dev/null +++ b/bytheway/src/main/java/cc/winboll/studio/bytheway/utils/GpsUtil.java @@ -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 + */ +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"; + } +}