From b3df8c77707d7235eaab0a8555ef72746f93395b Mon Sep 17 00:00:00 2001 From: LaizyBoy Date: Thu, 7 May 2026 02:45:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(gpsrelaysentinel):=20=E4=BD=BF=E7=94=A8SP?= =?UTF-8?q?=E6=A0=87=E8=AE=B0=E7=AE=A1=E7=90=86=E6=9C=8D=E5=8A=A1=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E5=B9=B6=E6=94=AF=E6=8C=81=E8=87=AA=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - onStartCommand返回START_STICKY实现服务自启动 - onStartCommand直接设置SP标记为启用,不检查现有标记 - onCreate时检查SP标记,已启用则自动启动GPS - onDestroy不再改变SP标记 - MainActivity stopService前先设置SP标记为不启用 --- .../studio/gpsrelaysentinel/MainActivity.java | 6 ++++++ .../studio/gpsrelaysentinel/MainService.java | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainActivity.java b/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainActivity.java index f452860..9026924 100644 --- a/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainActivity.java +++ b/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainActivity.java @@ -1,5 +1,6 @@ package cc.winboll.studio.gpsrelaysentinel; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; @@ -88,6 +89,11 @@ public class MainActivity extends AppCompatActivity { } private void stopService() { + // 先设置SP标记为不启用 + MainActivity.this.getSharedPreferences(MainService.PREF_NAME, Context.MODE_PRIVATE) + .edit() + .putBoolean(MainService.KEY_SERVICE_ENABLED, false) + .apply(); Intent intent = new Intent(MainActivity.this, MainService.class); stopService(intent); ToastUtils.show("GPS Service stopped"); diff --git a/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java b/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java index 6de85b6..8e3ec41 100644 --- a/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java +++ b/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java @@ -26,6 +26,8 @@ public class MainService extends Service { private NotificationCompat.Builder mNotificationBuilder; private static final String CHANNEL_ID = "gps_relay_channel"; private static final int NOTIFICATION_ID = 1; + private static final String PREF_NAME = "gps_relay_service_prefs"; + private static final String KEY_SERVICE_ENABLED = "service_enabled"; @Override public void onCreate() { @@ -33,15 +35,29 @@ public class MainService extends Service { LogUtils.d(TAG, "Service onCreate"); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); createNotificationChannel(); + if (isServiceEnabled()) { + LogUtils.d(TAG, "Service was enabled, starting GPS updates"); + run(); + } } @Override public int onStartCommand(Intent intent, int flags, int startId) { LogUtils.d(TAG, "Service onStartCommand"); + setServiceEnabled(true); run(); return START_STICKY; } + private boolean isServiceEnabled() { + return getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).getBoolean(KEY_SERVICE_ENABLED, false); + } + + private void setServiceEnabled(boolean enabled) { + getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit().putBoolean(KEY_SERVICE_ENABLED, enabled).apply(); + LogUtils.d(TAG, "Service enabled set to: " + enabled); + } + private void run() { if (mIsRunning) { LogUtils.d(TAG, "GPS updates already running");