mirror of
https://gitea.winboll.cc/ZhanGSKen/MyWinBoLL.git
synced 2026-08-14 05:27:11 +08:00
GpsUtil转型为GPS数据处理模块,封装数据库操作
- GpsUtil新增insertGps、queryAllGps、queryByTime、clearAllGps静态方法 - GpsDbHelper内嵌为GpsUtil私有内部类,外部不可直接访问 - 删除原GpsDbHelper.java和GpsDbManager.java独立文件 - GpsReceiveService和GpsListActivity改为通过GpsUtil访问数据库
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Mon Jul 06 19:59:58 CST 2026
|
||||
#Mon Jul 06 20:21:37 CST 2026
|
||||
stageCount=2
|
||||
libraryProject=
|
||||
baseVersion=15.20
|
||||
publishVersion=15.20.1
|
||||
buildCount=12
|
||||
buildCount=14
|
||||
baseBetaVersion=15.20.2
|
||||
|
||||
@@ -8,8 +8,8 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||
import cc.winboll.studio.bytheway.R;
|
||||
import cc.winboll.studio.bytheway.adapters.GpsAdapter;
|
||||
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;
|
||||
import java.util.List;
|
||||
|
||||
@@ -24,7 +24,6 @@ public class GpsListActivity extends AppCompatActivity {
|
||||
|
||||
private SwipeRefreshLayout swipeRefresh;
|
||||
private RecyclerView rvGps;
|
||||
private GpsDbManager dbManager;
|
||||
private GpsAdapter adapter;
|
||||
|
||||
@Override
|
||||
@@ -41,7 +40,6 @@ public class GpsListActivity extends AppCompatActivity {
|
||||
private void initView() {
|
||||
swipeRefresh = findViewById(R.id.swipe_refresh_layout);
|
||||
rvGps = findViewById(R.id.rv_gps_list);
|
||||
dbManager = new GpsDbManager(this);
|
||||
rvGps.setLayoutManager(new LinearLayoutManager(this));
|
||||
LogUtils.i("GpsListActivity", "控件初始化完成");
|
||||
}
|
||||
@@ -64,7 +62,7 @@ public class GpsListActivity extends AppCompatActivity {
|
||||
new Handler().postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final List<GpsBean> gpsDataList = dbManager.queryAllGps();
|
||||
final List<GpsBean> gpsDataList = GpsUtil.queryAllGps(GpsListActivity.this);
|
||||
LogUtils.i("GpsListActivity", "查询GPS数据条数:" + gpsDataList.size());
|
||||
|
||||
adapter = new GpsAdapter(GpsListActivity.this, gpsDataList);
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package cc.winboll.studio.bytheway.helpers;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
/**
|
||||
* GPS数据库创建、版本升级管理
|
||||
* @author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||
* 创建时间:2026-07-02 15:00:00
|
||||
* 最后编辑时间:2026-07-02 16:35:41
|
||||
*/
|
||||
public class GpsDbHelper extends SQLiteOpenHelper {
|
||||
|
||||
private static final String DB_NAME = "GpsLocation.db";
|
||||
private static final int DB_VERSION = 1;
|
||||
public static final String TABLE_GPS = "gps_table";
|
||||
|
||||
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_GPS + " ("
|
||||
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
+ "latitude REAL,"
|
||||
+ "longitude REAL,"
|
||||
+ "accuracy REAL,"
|
||||
+ "altitude REAL,"
|
||||
+ "speed REAL,"
|
||||
+ "receive_time INTEGER"
|
||||
+ ")";
|
||||
|
||||
public GpsDbHelper(final Context context) {
|
||||
super(context, DB_NAME, null, DB_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(final SQLiteDatabase db) {
|
||||
db.execSQL(CREATE_TABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GPS);
|
||||
onCreate(db);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
package cc.winboll.studio.bytheway.helpers;
|
||||
|
||||
import android.content.ContentValues;
|
||||
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;
|
||||
|
||||
/**
|
||||
* GPS SQLite数据库数据管理工具
|
||||
* 负责本地定位轨迹插入、查询、时间段筛选、清空操作
|
||||
* @author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||
* 创建时间:2026-07-02 15:03:00
|
||||
* 最后编辑时间:2026-07-02 16:35:41
|
||||
*/
|
||||
public class GpsDbManager {
|
||||
|
||||
private static final String TABLE_GPS = "gps_table";
|
||||
private final GpsDbHelper dbHelper;
|
||||
|
||||
public GpsDbManager(final Context context) {
|
||||
dbHelper = new GpsDbHelper(context);
|
||||
}
|
||||
|
||||
//插入单条GPS定位数据
|
||||
public void insertGps(final GpsBean bean) {
|
||||
final SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||
final ContentValues values = new ContentValues();
|
||||
|
||||
values.put("latitude", bean.getLatitude());
|
||||
values.put("longitude", bean.getLongitude());
|
||||
values.put("accuracy", bean.getAccuracy());
|
||||
values.put("altitude", bean.getAltitude());
|
||||
values.put("speed", bean.getSpeed());
|
||||
values.put("receive_time", bean.getReceiveTime());
|
||||
|
||||
db.insert(TABLE_GPS, null, values);
|
||||
LogUtils.i("GpsDbManager", "GPS数据插入成功");
|
||||
db.close();
|
||||
}
|
||||
|
||||
//查询全部GPS记录,按时间倒序排列
|
||||
public List<GpsBean> queryAllGps() {
|
||||
final List<GpsBean> list = new ArrayList<GpsBean>();
|
||||
final SQLiteDatabase db = dbHelper.getReadableDatabase();
|
||||
|
||||
final Cursor cursor = db.query(TABLE_GPS, null, null, null, null, null, "receive_time DESC");
|
||||
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
list.add(GpsUtil.parseFromCursor(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
LogUtils.i("GpsDbManager", "查询全部GPS条数:" + list.size());
|
||||
cursor.close();
|
||||
db.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
//根据时间区间查询GPS轨迹
|
||||
public List<GpsBean> queryByTime(final long startTime, final long endTime) {
|
||||
final List<GpsBean> list = new ArrayList<GpsBean>();
|
||||
final SQLiteDatabase db = dbHelper.getReadableDatabase();
|
||||
|
||||
final String where = "receive_time >= ? AND receive_time <= ?";
|
||||
final String[] args = {String.valueOf(startTime), String.valueOf(endTime)};
|
||||
|
||||
final Cursor cursor = db.query(TABLE_GPS, null, where, args, null, null, "receive_time ASC");
|
||||
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
list.add(GpsUtil.parseFromCursor(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
LogUtils.i("GpsDbManager", "时间段查询GPS条数:" + list.size());
|
||||
cursor.close();
|
||||
db.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
//清空所有本地GPS历史数据
|
||||
public void clearAllGps() {
|
||||
final SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||
db.delete(TABLE_GPS, null, null);
|
||||
LogUtils.i("GpsDbManager", "已清空全部GPS数据库数据");
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import android.content.IntentFilter;
|
||||
import android.os.Build;
|
||||
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;
|
||||
@@ -28,7 +27,6 @@ public class GpsReceiveService extends Service {
|
||||
private static final String CHANNEL_ID = "gps_receive_service_channel";
|
||||
|
||||
private static boolean serviceRunning = false;
|
||||
private GpsDbManager dbManager;
|
||||
private BroadcastReceiver gpsServiceBroadcastReceiver;
|
||||
|
||||
/**
|
||||
@@ -69,7 +67,6 @@ public class GpsReceiveService extends Service {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
serviceRunning = true;
|
||||
dbManager = new GpsDbManager(getApplicationContext());
|
||||
registerGpsServiceBroadcast();
|
||||
startForeground(NOTIFICATION_ID, createNotification());
|
||||
LogUtils.i("GpsReceiveService", "GPS定位消息接收服务创建完成");
|
||||
@@ -116,7 +113,7 @@ public class GpsReceiveService extends Service {
|
||||
*/
|
||||
private void parseAndSaveGpsData(final Intent intent) {
|
||||
final GpsBean bean = GpsUtil.parseFromIntent(intent);
|
||||
dbManager.insertGps(bean);
|
||||
GpsUtil.insertGps(getApplicationContext(), bean);
|
||||
LogUtils.d("GpsReceiveService", "收到GPS广播,已保存定位数据");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
package cc.winboll.studio.bytheway.utils;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import cc.winboll.studio.bytheway.models.GpsBean;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* GPS数据处理工具类
|
||||
* 集合项目中所有GPS数据处理方法,供外部统一调用
|
||||
* GPS数据处理模块
|
||||
* 集合GPS数据解析、格式化、数据库存取等所有操作,外部统一通过本类访问
|
||||
* @author ZhanGSKen<zhangsken@qq.com>
|
||||
*/
|
||||
public class GpsUtil {
|
||||
|
||||
private static final String TAG = "GpsUtil";
|
||||
|
||||
// Intent广播中的GPS数据键名
|
||||
public static final String KEY_LATITUDE = "latitude";
|
||||
public static final String KEY_LONGITUDE = "longitude";
|
||||
@@ -25,6 +34,8 @@ public class GpsUtil {
|
||||
// 时间格式
|
||||
private static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
// ======================== 数据解析方法 ========================
|
||||
|
||||
/**
|
||||
* 从Intent广播数据中解析GPS定位信息,构造GpsBean对象
|
||||
* @param intent GPS定位广播Intent
|
||||
@@ -66,9 +77,10 @@ public class GpsUtil {
|
||||
return bean;
|
||||
}
|
||||
|
||||
// ======================== 格式化方法 ========================
|
||||
|
||||
/**
|
||||
* 将时间戳格式化为可读时间字符串
|
||||
* 格式:yyyy-MM-dd HH:mm:ss
|
||||
* @param timestamp 毫秒时间戳
|
||||
* @return 格式化后的时间字符串
|
||||
*/
|
||||
@@ -81,7 +93,7 @@ public class GpsUtil {
|
||||
* 格式化经纬度显示文本
|
||||
* @param latitude 纬度
|
||||
* @param longitude 经度
|
||||
* @return 格式化后的经纬度字符串,如"纬度:39.9 经度:116.4"
|
||||
* @return 格式化后的经纬度字符串
|
||||
*/
|
||||
public static String formatLatLng(final double latitude, final double longitude) {
|
||||
return "纬度:" + latitude + " 经度:" + longitude;
|
||||
@@ -92,7 +104,7 @@ public class GpsUtil {
|
||||
* @param accuracy 精度(米)
|
||||
* @param altitude 海拔(米)
|
||||
* @param speed 速度(m/s)
|
||||
* @return 格式化后的字符串,如"精度:10.0m 海拔:100.0m 速度:5.0m/s"
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public static String formatOther(final float accuracy, final double altitude, final float speed) {
|
||||
return "精度:" + accuracy + "m 海拔:" + altitude + "m 速度:" + speed + "m/s";
|
||||
@@ -110,9 +122,126 @@ public class GpsUtil {
|
||||
/**
|
||||
* 格式化速度显示文本(km/h)
|
||||
* @param speedMps 速度(m/s)
|
||||
* @return 格式化后的速度字符串,如"18.0km/h"
|
||||
* @return 格式化后的速度字符串
|
||||
*/
|
||||
public static String formatSpeed(final float speedMps) {
|
||||
return speedToKmh(speedMps) + "km/h";
|
||||
}
|
||||
|
||||
// ======================== 数据库操作方法 ========================
|
||||
|
||||
/**
|
||||
* 插入单条GPS定位数据
|
||||
* @param context 上下文
|
||||
* @param bean GPS数据对象
|
||||
*/
|
||||
public static void insertGps(final Context context, final GpsBean bean) {
|
||||
final GpsDbHelper helper = new GpsDbHelper(context);
|
||||
final SQLiteDatabase db = helper.getWritableDatabase();
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put("latitude", bean.getLatitude());
|
||||
values.put("longitude", bean.getLongitude());
|
||||
values.put("accuracy", bean.getAccuracy());
|
||||
values.put("altitude", bean.getAltitude());
|
||||
values.put("speed", bean.getSpeed());
|
||||
values.put("receive_time", bean.getReceiveTime());
|
||||
db.insert(GpsDbHelper.TABLE_GPS, null, values);
|
||||
LogUtils.i(TAG, "GPS数据插入成功");
|
||||
db.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询全部GPS记录,按时间倒序排列
|
||||
* @param context 上下文
|
||||
* @return GPS数据列表
|
||||
*/
|
||||
public static List<GpsBean> queryAllGps(final Context context) {
|
||||
final List<GpsBean> list = new ArrayList<GpsBean>();
|
||||
final GpsDbHelper helper = new GpsDbHelper(context);
|
||||
final SQLiteDatabase db = helper.getReadableDatabase();
|
||||
final Cursor cursor = db.query(GpsDbHelper.TABLE_GPS, null, null, null, null, null, "receive_time DESC");
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
list.add(parseFromCursor(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
LogUtils.i(TAG, "查询全部GPS条数:" + list.size());
|
||||
cursor.close();
|
||||
db.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间区间查询GPS轨迹
|
||||
* @param context 上下文
|
||||
* @param startTime 起始时间戳
|
||||
* @param endTime 结束时间戳
|
||||
* @return GPS数据列表
|
||||
*/
|
||||
public static List<GpsBean> queryByTime(final Context context, final long startTime, final long endTime) {
|
||||
final List<GpsBean> list = new ArrayList<GpsBean>();
|
||||
final GpsDbHelper helper = new GpsDbHelper(context);
|
||||
final SQLiteDatabase db = helper.getReadableDatabase();
|
||||
final String where = "receive_time >= ? AND receive_time <= ?";
|
||||
final String[] args = {String.valueOf(startTime), String.valueOf(endTime)};
|
||||
final Cursor cursor = db.query(GpsDbHelper.TABLE_GPS, null, where, args, null, null, "receive_time ASC");
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
list.add(parseFromCursor(cursor));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
LogUtils.i(TAG, "时间段查询GPS条数:" + list.size());
|
||||
cursor.close();
|
||||
db.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有本地GPS历史数据
|
||||
* @param context 上下文
|
||||
*/
|
||||
public static void clearAllGps(final Context context) {
|
||||
final GpsDbHelper helper = new GpsDbHelper(context);
|
||||
final SQLiteDatabase db = helper.getWritableDatabase();
|
||||
db.delete(GpsDbHelper.TABLE_GPS, null, null);
|
||||
LogUtils.i(TAG, "已清空全部GPS数据库数据");
|
||||
db.close();
|
||||
}
|
||||
|
||||
// ======================== 内部类:数据库管理 ========================
|
||||
|
||||
/**
|
||||
* SQLite数据库创建、版本升级管理
|
||||
*/
|
||||
private static class GpsDbHelper extends SQLiteOpenHelper {
|
||||
|
||||
private static final String DB_NAME = "GpsLocation.db";
|
||||
private static final int DB_VERSION = 1;
|
||||
private static final String TABLE_GPS = "gps_table";
|
||||
|
||||
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_GPS + " ("
|
||||
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
+ "latitude REAL,"
|
||||
+ "longitude REAL,"
|
||||
+ "accuracy REAL,"
|
||||
+ "altitude REAL,"
|
||||
+ "speed REAL,"
|
||||
+ "receive_time INTEGER"
|
||||
+ ")";
|
||||
|
||||
public GpsDbHelper(final Context context) {
|
||||
super(context, DB_NAME, null, DB_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(final SQLiteDatabase db) {
|
||||
db.execSQL(CREATE_TABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GPS);
|
||||
onCreate(db);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user