From 33f74f8226f3c0f8bf981994b97dd8ad9ee97ead Mon Sep 17 00:00:00 2001 From: MiMo Date: Thu, 9 Jul 2026 20:07:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DGpsReceiveService=20GP?= =?UTF-8?q?S=E8=AE=A1=E6=95=B0=E5=80=BC=E4=B8=8D=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将GPS接收方式从BroadcastReceiver替换为LocationManager+LocationListener, 解决因无广播发送方导致计数值永远为0的问题。 - 使用LocationManager.requestLocationUpdates主动获取GPS定位 - 新增currentGpsBean内存缓存当前最新GPS数据 - 通知栏显示GPS数据信息(经纬度/精度)和接收次数 - 新增getCurrentGpsBean()供外部获取当前GPS数据 --- bytheway/build.properties | 4 +- .../bytheway/services/GpsReceiveService.java | 124 ++++++++++++++---- 2 files changed, 98 insertions(+), 30 deletions(-) diff --git a/bytheway/build.properties b/bytheway/build.properties index d39f236..9ab4e1d 100644 --- a/bytheway/build.properties +++ b/bytheway/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Tue Jul 07 00:40:29 CST 2026 +#Thu Jul 09 20:03:50 CST 2026 stageCount=2 libraryProject= baseVersion=15.20 publishVersion=15.20.1 -buildCount=22 +buildCount=29 baseBetaVersion=15.20.2 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 3012051..b5b9a18 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 @@ -4,11 +4,13 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Service; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.os.Build; +import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; import cc.winboll.studio.bytheway.models.GpsBean; @@ -18,7 +20,7 @@ import cc.winboll.studio.libappbase.LogUtils; /** * @Author 豆包&BigPickle&ZhanGSKen * @Date 2026/07/02 15:33 - * @Describe 动态注册定位广播,接收GPS数据并持久化存入SQLite数据库。单例独占模式,自带重复启动拦截,对外静态统一启停接口 + * @Describe GPS定位接收服务,通过LocationManager获取GPS数据并持久化存入SQLite数据库。单例独占模式,自带重复启动拦截,对外静态统一启停接口 */ public class GpsReceiveService extends Service { @@ -27,8 +29,11 @@ public class GpsReceiveService extends Service { private static final String CHANNEL_ID = "gps_receive_service_channel"; private static boolean serviceRunning = false; + private static GpsReceiveService instance; private int gpsReceiveCount = 0; - private BroadcastReceiver gpsServiceBroadcastReceiver; + private GpsBean currentGpsBean; + private LocationManager locationManager; + private LocationListener locationListener; /** * 获取GPS定位服务运行状态 @@ -38,6 +43,14 @@ public class GpsReceiveService extends Service { return serviceRunning; } + /** + * 获取当前最新的GPS定位数据 + * @return 当前GPS数据,未收到定位时返回null + */ + public static GpsBean getCurrentGpsBean() { + return serviceRunning ? instance.currentGpsBean : null; + } + /** * 外部统一静态启动服务 */ @@ -67,15 +80,17 @@ public class GpsReceiveService extends Service { @Override public void onCreate() { super.onCreate(); + instance = this; serviceRunning = true; gpsReceiveCount = 0; - registerGpsServiceBroadcast(); - startForeground(NOTIFICATION_ID, createNotification(gpsReceiveCount)); + currentGpsBean = null; + startGpsLocationUpdates(); + startForeground(NOTIFICATION_ID, createNotification()); LogUtils.i("GpsReceiveService", "GPS定位消息接收服务创建完成"); Toast.makeText(this, "GPS定位消息接收服务已开启", Toast.LENGTH_SHORT).show(); } - private Notification createNotification(final int count) { + private Notification createNotification() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, @@ -85,9 +100,18 @@ public class GpsReceiveService extends Service { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.createNotificationChannel(channel); } + String contentText; + if (currentGpsBean != null) { + contentText = "已接收 " + gpsReceiveCount + " 次 | " + + "纬度:" + currentGpsBean.getLatitude() + + " 经度:" + currentGpsBean.getLongitude() + + " 精度:" + currentGpsBean.getAccuracy() + "m"; + } else { + contentText = "等待GPS定位数据..."; + } return new Notification.Builder(this, CHANNEL_ID) .setContentTitle("ByTheWay") - .setContentText("已接收GPS数据 " + count + " 次") + .setContentText(contentText) .setSmallIcon(android.R.drawable.ic_menu_mylocation) .setOngoing(true) .build(); @@ -98,35 +122,67 @@ public class GpsReceiveService extends Service { */ private void updateNotification() { final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - manager.notify(NOTIFICATION_ID, createNotification(gpsReceiveCount)); + manager.notify(NOTIFICATION_ID, createNotification()); } /** - * 动态注册GPS定位广播接收器 + * 启动GPS定位更新监听 */ - private void registerGpsServiceBroadcast() { - gpsServiceBroadcastReceiver = new BroadcastReceiver() { + private void startGpsLocationUpdates() { + locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); + locationListener = new LocationListener() { @Override - public void onReceive(final Context context, final Intent intent) { - parseAndSaveGpsData(intent); + public void onLocationChanged(final Location location) { + handleGpsLocation(location); + } + + @Override + public void onStatusChanged(final String provider, final int status, final Bundle extras) { + LogUtils.d("GpsReceiveService", "GPS状态变化: " + provider + ", status=" + status); + } + + @Override + public void onProviderEnabled(final String provider) { + LogUtils.d("GpsReceiveService", "GPS提供者已启用: " + provider); + } + + @Override + public void onProviderDisabled(final String provider) { + LogUtils.d("GpsReceiveService", "GPS提供者已禁用: " + provider); } }; - final IntentFilter filter = new IntentFilter(); - //GPS定位广播动作,与你定位发送广播对应 - filter.addAction("android.location.GPS_LOCATION_BROADCAST"); - registerReceiver(gpsServiceBroadcastReceiver, filter); - LogUtils.i("GpsServiceBroadcastReceiver", "GPS广播接收动态注册成功"); + try { + // 最小时间间隔1秒,最小距离间隔0米,确保每次位置更新都能接收 + locationManager.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + 1000L, + 0f, + locationListener + ); + LogUtils.i("GpsReceiveService", "GPS定位监听已启动"); + } catch (SecurityException e) { + LogUtils.e("GpsReceiveService", "缺少GPS定位权限: " + e.getMessage()); + } catch (Exception e) { + LogUtils.e("GpsReceiveService", "启动GPS定位监听失败: " + e.getMessage()); + } } /** - * 解析广播GPS数据并写入SQLite数据库 + * 处理GPS定位数据,写入SQLite数据库并更新通知栏 */ - private void parseAndSaveGpsData(final Intent intent) { - final GpsBean bean = GpsUtil.parseFromIntent(intent); + private void handleGpsLocation(final Location location) { + final GpsBean bean = new GpsBean(); + bean.setLatitude(location.getLatitude()); + bean.setLongitude(location.getLongitude()); + bean.setAccuracy(location.getAccuracy()); + bean.setAltitude(location.getAltitude()); + bean.setSpeed(location.getSpeed()); + bean.setReceiveTime(System.currentTimeMillis()); + currentGpsBean = bean; GpsUtil.insertGps(getApplicationContext(), bean); gpsReceiveCount++; updateNotification(); - LogUtils.d("GpsReceiveService", "收到GPS广播,已保存定位数据,累计接收 " + gpsReceiveCount + " 次"); + LogUtils.d("GpsReceiveService", "收到GPS定位,已保存数据,累计接收 " + gpsReceiveCount + " 次"); } @Override @@ -142,15 +198,27 @@ public class GpsReceiveService extends Service { @Override public void onDestroy() { + stopGpsLocationUpdates(); stopForeground(true); serviceRunning = false; - //反注册广播,防止内存泄漏 - if (gpsServiceBroadcastReceiver != null) { - unregisterReceiver(gpsServiceBroadcastReceiver); - } - LogUtils.i("GpsReceiveService", "GPS接收服务已销毁,广播已解绑"); + instance = null; + LogUtils.i("GpsReceiveService", "GPS接收服务已销毁,定位监听已停止"); Toast.makeText(this, "GPS定位服务已关闭", Toast.LENGTH_SHORT).show(); super.onDestroy(); } + /** + * 停止GPS定位更新监听 + */ + private void stopGpsLocationUpdates() { + if (locationManager != null && locationListener != null) { + try { + locationManager.removeUpdates(locationListener); + LogUtils.i("GpsReceiveService", "GPS定位监听已移除"); + } catch (SecurityException e) { + LogUtils.e("GpsReceiveService", "移除GPS监听异常: " + e.getMessage()); + } + } + } + }