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"); } }