- // Start the realtime monitoring service as a foreground service, which is required because the worker is running in the background.
- applicationContext.startForegroundService(Intent(applicationContext, RealtimeMonitoringService::class.java))
+ // Get a handle for the shared preferences.
+ val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
+
+ // Check to see if realtime monitoring is enabled. Sometimes the shared preferences can't return a value in time, because Android sucks.
+ // So, the default value is set to true, which is the safest value if the shared preferences can't be queried.
+ if (sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true)) { // Realtime monitoring is enabled.
+ // Start the realtime monitoring service as a foreground service, which is required because the worker is running in the background.
+ applicationContext.startForegroundService(Intent(applicationContext, RealtimeMonitoringService::class.java))
+ } else { // Realtime monitoring is disabled.
+ // Stop the realtime monitoring service.
+ applicationContext.stopService(Intent(applicationContext, RealtimeMonitoringService::class.java))
+
+ // Cancel the realtime listener work request.
+ WorkManager.getInstance(applicationContext).cancelUniqueWork(applicationContext.getString(R.string.register_listener_work_request))
+ }