From 11aee7e3733e85fd8e37aed23931127ebfe2695b Mon Sep 17 00:00:00 2001 From: LaizyBoy Date: Thu, 7 May 2026 02:02:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gpsrelaysentinel):=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84MainService=E6=B7=BB=E5=8A=A0run=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E7=AE=A1=E7=90=86GPS=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将GPS定位申请逻辑从onCreate()转移到新增的run()函数 - onStartCommand()调用run()启动GPS监听 - 添加mIsRunning标志防止重复启动 - onCreate()不再直接初始化定位功能 - onDestroy()中重置mIsRunning标志 --- .../winboll/studio/gpsrelaysentinel/MainService.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 1bad227..976a6db 100644 --- a/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java +++ b/gpsrelaysentinel/src/main/java/cc/winboll/studio/gpsrelaysentinel/MainService.java @@ -15,21 +15,26 @@ public class MainService extends Service { public static final String TAG = "MainService"; private LocationManager mLocationManager; private LocationListener mLocationListener; + private boolean mIsRunning = false; @Override public void onCreate() { super.onCreate(); LogUtils.d(TAG, "Service onCreate"); - initLocation(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { LogUtils.d(TAG, "Service onStartCommand"); + run(); return START_STICKY; } - private void initLocation() { + private void run() { + if (mIsRunning) { + LogUtils.d(TAG, "GPS updates already running"); + return; + } mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mLocationListener = new LocationListener() { @Override @@ -62,6 +67,7 @@ public class MainService extends Service { mLocationListener ); LogUtils.d(TAG, "GPS location updates requested"); + mIsRunning = true; } } catch (SecurityException e) { LogUtils.e(TAG, "Permission denied: " + e.getMessage()); @@ -83,6 +89,7 @@ public class MainService extends Service { LogUtils.e(TAG, "Permission denied when removing updates: " + e.getMessage()); } } + mIsRunning = false; LogUtils.d(TAG, "Service onDestroy"); } }