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