GPS接收服务增加数据接收次数计数并在通知栏实时显示

- onCreate初始化gpsReceiveCount计数器归零
- parseAndSaveGpsData每次接收数据后递增计数并刷新通知栏
- 通知栏文本更新为"已接收GPS数据 N 次"
This commit is contained in:
MiMo
2026-07-07 00:13:26 +08:00
parent 7c8800f873
commit d4fddaecf9
2 changed files with 18 additions and 6 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Mon Jul 06 20:21:37 CST 2026
#Mon Jul 06 20:33:32 CST 2026
stageCount=2
libraryProject=
baseVersion=15.20
publishVersion=15.20.1
buildCount=14
buildCount=16
baseBetaVersion=15.20.2
@@ -27,6 +27,7 @@ public class GpsReceiveService extends Service {
private static final String CHANNEL_ID = "gps_receive_service_channel";
private static boolean serviceRunning = false;
private int gpsReceiveCount = 0;
private BroadcastReceiver gpsServiceBroadcastReceiver;
/**
@@ -67,13 +68,14 @@ public class GpsReceiveService extends Service {
public void onCreate() {
super.onCreate();
serviceRunning = true;
gpsReceiveCount = 0;
registerGpsServiceBroadcast();
startForeground(NOTIFICATION_ID, createNotification());
startForeground(NOTIFICATION_ID, createNotification(gpsReceiveCount));
LogUtils.i("GpsReceiveService", "GPS定位消息接收服务创建完成");
Toast.makeText(this, "GPS定位消息接收服务已开启", Toast.LENGTH_SHORT).show();
}
private Notification createNotification() {
private Notification createNotification(final int count) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
@@ -85,12 +87,20 @@ public class GpsReceiveService extends Service {
}
return new Notification.Builder(this, CHANNEL_ID)
.setContentTitle("ByTheWay")
.setContentText("GPS定位消息接收服务运行中")
.setContentText("已接收GPS数据 " + count + "")
.setSmallIcon(android.R.drawable.ic_menu_mylocation)
.setOngoing(true)
.build();
}
/**
* 更新前台服务通知栏显示
*/
private void updateNotification() {
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, createNotification(gpsReceiveCount));
}
/**
* 动态注册GPS定位广播接收器
*/
@@ -114,7 +124,9 @@ public class GpsReceiveService extends Service {
private void parseAndSaveGpsData(final Intent intent) {
final GpsBean bean = GpsUtil.parseFromIntent(intent);
GpsUtil.insertGps(getApplicationContext(), bean);
LogUtils.d("GpsReceiveService", "收到GPS广播,已保存定位数据");
gpsReceiveCount++;
updateNotification();
LogUtils.d("GpsReceiveService", "收到GPS广播,已保存定位数据,累计接收 " + gpsReceiveCount + "");
}
@Override