Jelajahi Sumber

Make sure to stop background timers.

Thomas Chef 3 tahun lalu
induk
melakukan
a45499e578

+ 9 - 5
app/src/main/java/com/flacksta/chef/journeygpstracker/TrackingService.kt

@@ -31,6 +31,7 @@ class TrackingService : Service() {
     private lateinit var trackApp : JourneyGpsTrackerApplication
     private lateinit var mNotificationManager: NotificationManager
     private lateinit var mNotifyBuilder : NotificationCompat.Builder
+    private lateinit var mBackendClient : BackendClient
 
     private val mNotificationId = 123
     private val CHANNEL_ID = "JourneyGPSTracker_Notifications"
@@ -72,6 +73,7 @@ class TrackingService : Service() {
     override fun onDestroy() {
         super.onDestroy()
         Log.i(mTag,"onDestroy()")
+        isServiceStarted = false
         Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show()
     }
 
@@ -133,10 +135,11 @@ class TrackingService : Service() {
         repo.insert(d)
     }
 
-    fun startNotificationUpdateTimer() = GlobalScope.launch {
-        while (true) {
+    private fun startNotificationUpdateTimer() = GlobalScope.launch {
+        while( isServiceStarted ) {
+            Log.i(mTag,"Update notification")
             updateNotification()
-            delay(1000)
+            delay(10000)
         }
     }
 
@@ -147,12 +150,14 @@ class TrackingService : Service() {
         isServiceStarted = true
         setServiceState(this, ServiceState.STARTED)
         requestLocationUpdates()
-        BackendClient()
+        mBackendClient = BackendClient()
         startNotificationUpdateTimer()
     }
 
     private fun stopService() {
         Log.i(mTag,"stopService()")
+        isServiceStarted = false
+        mBackendClient.stopSendingUpdates()
         try {
             stopForeground(true)
             stopSelf()
@@ -160,7 +165,6 @@ class TrackingService : Service() {
         } catch (e: Exception) {
             Log.e(mTag, "Service stopped without being started: ${e.message}")
         }
-        isServiceStarted = false
         setServiceState(this, ServiceState.STOPPED)
     }
 

+ 12 - 1
app/src/main/java/com/flacksta/chef/journeygpstracker/backend/BackendClient.kt

@@ -25,19 +25,25 @@ class BackendClient {
 
     private val mTag: String = "TrackingService"
     private var trackApp : JourneyGpsTrackerApplication = JourneyGpsTrackerApplication()
+    private var mRunning : Boolean = true
 
     init {
         startTimer()
     }
 
+    public fun stopSendingUpdates() {
+        mRunning = false
+    }
+
     private fun startTimer() {
         val fixedRateTimer = fixedRateTimer(name = "hello-timer",
                 initialDelay = 10000, period = 60000) {
 
             val dao: GpsDataDao = trackApp.database.gpsDataDao()
+            val pos: List<GpsData> = dao.getUnsentGpsPositions()
 
             Log.i(mTag, "Sending data....")
-            val pos: List<GpsData> = dao.getUnsentGpsPositions()
+
             if (pos.isNotEmpty()) {
                 val rawJson: String = convertPosListToRawJson(pos)
                 val retVal: Boolean = sendGpsPosDataToBackendServer(rawJson)
@@ -51,6 +57,11 @@ class BackendClient {
 
             // Delete of older data more than 1h old and already uploaded to server
             dao.deleteOldAndUploaded(System.currentTimeMillis().minus(60L * 60L * 1000L))
+
+            if( !mRunning && pos.isEmpty()) {
+                Log.i(mTag,"Stopping background timer.")
+                this.cancel()
+            }
         }
     }