From 0e90f40f0f037792724094cc0a8be8e09444e841 Mon Sep 17 00:00:00 2001 From: LaizyBoy Date: Thu, 7 May 2026 02:15:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(gpsrelaysentinel):=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=89=8D=E5=8F=B0=E6=9C=8D=E5=8A=A1=E9=80=9A=E7=9F=A5=E5=B9=B6?= =?UTF-8?q?=E9=80=9A=E8=BF=87Switch=E6=8E=A7=E5=88=B6=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=90=AF=E5=81=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加FOREGROUND_SERVICE权限支持前台服务 - 使用startForegroundService替代startService启动服务 - 实现前台服务通知,实时显示GPS经纬度数据 - 在MainActivity添加Switch开关控制服务启停 - GPS位置更新时通过updateNotification实时更新通知内容 - 创建通知渠道适配Android O及以上版本 --- gpsrelaysentinel/src/main/AndroidManifest.xml | 1 + .../studio/gpsrelaysentinel/MainActivity.java | 18 ++++++++ .../studio/gpsrelaysentinel/MainService.java | 46 ++++++++++++++++++- .../src/main/res/layout/activity_main.xml | 7 +++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/gpsrelaysentinel/src/main/AndroidManifest.xml b/gpsrelaysentinel/src/main/AndroidManifest.xml index 2c92a4a..4966b18 100644 --- a/gpsrelaysentinel/src/main/AndroidManifest.xml +++ b/gpsrelaysentinel/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + = Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel( + CHANNEL_ID, + "GPS Relay Service", + NotificationManager.IMPORTANCE_LOW + ); + channel.setDescription("GPS Relay Sentinel service channel"); + mNotificationManager.createNotificationChannel(channel); + } + } + + private void startForegroundNotification() { + mNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle("GPS Relay Service") + .setContentText("Waiting for GPS data...") + .setSmallIcon(android.R.drawable.ic_menu_mylocation) + .setOngoing(true); + Notification notification = mNotificationBuilder.build(); + startForeground(NOTIFICATION_ID, notification); + } + + private void updateNotification(String gpsInfo) { + if (mNotificationBuilder != null) { + mNotificationBuilder.setContentText(gpsInfo); + mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build()); + } + } + @Override public IBinder onBind(Intent intent) { return null; diff --git a/gpsrelaysentinel/src/main/res/layout/activity_main.xml b/gpsrelaysentinel/src/main/res/layout/activity_main.xml index 5e86607..4a333f2 100644 --- a/gpsrelaysentinel/src/main/res/layout/activity_main.xml +++ b/gpsrelaysentinel/src/main/res/layout/activity_main.xml @@ -32,6 +32,13 @@ android:text="GPSRelaySentinel" android:textAppearance="?android:attr/textAppearanceLarge"/> + +