From e443e5454845dcd32aab35e1134e687ea217eba6 Mon Sep 17 00:00:00 2001 From: BigPickle Date: Thu, 2 Jul 2026 16:38:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E5=90=88=E6=A1=86=E6=9E=B6=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E6=93=8D=E4=BD=9C=E6=B5=81=E7=A8=8B=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bytheway/build.properties | 4 +-- bytheway/src/main/AndroidManifest.xml | 3 ++ .../winboll/studio/bytheway/MainActivity.java | 7 ++++ .../bytheway/activities/SettingsActivity.java | 27 +++++++++----- .../bytheway/services/GpsReceiveService.java | 35 +++++++++++++++++-- .../src/main/res/layout/activity_settings.xml | 12 +++++-- bytheway/src/main/res/menu/activity_main.xml | 11 ++++++ bytheway/src/main/res/values/strings.xml | 2 +- 8 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 bytheway/src/main/res/menu/activity_main.xml diff --git a/bytheway/build.properties b/bytheway/build.properties index dfe5a42..dcbf11e 100644 --- a/bytheway/build.properties +++ b/bytheway/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Thu Jul 02 07:36:18 GMT 2026 +#Thu Jul 02 16:21:25 HKT 2026 stageCount=0 libraryProject= baseVersion=15.20 publishVersion=15.20.0 -buildCount=22 +buildCount=28 baseBetaVersion=15.20.1 diff --git a/bytheway/src/main/AndroidManifest.xml b/bytheway/src/main/AndroidManifest.xml index e6d433d..8a4f318 100644 --- a/bytheway/src/main/AndroidManifest.xml +++ b/bytheway/src/main/AndroidManifest.xml @@ -12,6 +12,9 @@ + + + - * @Date 2026/07/02 15:38 - * @Describe 应用设置窗口 - */ public class SettingsActivity extends Activity { - + public static final String TAG = "SettingsActivity"; - + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); - + + Switch swGpsService = findViewById(R.id.sw_gps_service); + swGpsService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) { + GpsReceiveService.startGpsService(SettingsActivity.this); + } else { + GpsReceiveService.stopGpsService(SettingsActivity.this); + } + } + }); } - + } \ No newline at end of file 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 5c1946f..5699fc2 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 @@ -1,11 +1,16 @@ package cc.winboll.studio.bytheway.services; +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.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.libappbase.LogUtils; @@ -16,9 +21,11 @@ import cc.winboll.studio.libappbase.LogUtils; * @Describe 动态注册定位广播,接收GPS数据并持久化存入SQLite数据库。单例独占模式,自带重复启动拦截,对外静态统一启停接口 */ public class GpsReceiveService extends Service { - + public static final String TAG = "GpsReceiveService"; - + private static final int NOTIFICATION_ID = 1001; + private static final String CHANNEL_ID = "gps_service_channel"; + private static boolean serviceRunning = false; private GpsDbManager dbManager; private BroadcastReceiver gpsBroadcastReceiver; @@ -55,7 +62,27 @@ public class GpsReceiveService extends Service { serviceRunning = true; dbManager = new GpsDbManager(getApplicationContext()); registerGpsBroadcast(); + startForeground(NOTIFICATION_ID, createNotification()); LogUtils.i("GpsReceiveService", "GPS接收服务创建完成"); + Toast.makeText(this, "GPS定位服务已开启", Toast.LENGTH_SHORT).show(); + } + + private Notification createNotification() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel( + CHANNEL_ID, + "GPS定位服务", + NotificationManager.IMPORTANCE_LOW + ); + NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + manager.createNotificationChannel(channel); + } + return new Notification.Builder(this, CHANNEL_ID) + .setContentTitle("ByTheWay") + .setContentText("GPS定位服务运行中") + .setSmallIcon(android.R.drawable.ic_menu_mylocation) + .setOngoing(true) + .build(); } /** @@ -111,13 +138,15 @@ public class GpsReceiveService extends Service { @Override public void onDestroy() { - super.onDestroy(); + stopForeground(true); serviceRunning = false; //反注册广播,防止内存泄漏 if (gpsBroadcastReceiver != null) { unregisterReceiver(gpsBroadcastReceiver); } LogUtils.i("GpsReceiveService", "GPS接收服务已销毁,广播已解绑"); + Toast.makeText(this, "GPS定位服务已关闭", Toast.LENGTH_SHORT).show(); + super.onDestroy(); } } diff --git a/bytheway/src/main/res/layout/activity_settings.xml b/bytheway/src/main/res/layout/activity_settings.xml index 6684a21..cf8394f 100644 --- a/bytheway/src/main/res/layout/activity_settings.xml +++ b/bytheway/src/main/res/layout/activity_settings.xml @@ -1,9 +1,15 @@ - + android:layout_height="match_parent" + android:padding="16dp"> + + + \ No newline at end of file diff --git a/bytheway/src/main/res/menu/activity_main.xml b/bytheway/src/main/res/menu/activity_main.xml new file mode 100644 index 0000000..6a26831 --- /dev/null +++ b/bytheway/src/main/res/menu/activity_main.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/bytheway/src/main/res/values/strings.xml b/bytheway/src/main/res/values/strings.xml index dde8f6b..6a248fd 100644 --- a/bytheway/src/main/res/values/strings.xml +++ b/bytheway/src/main/res/values/strings.xml @@ -1,4 +1,4 @@ ByTheWay - + 设置