import com.stoutner.privacycell.services.RealtimeMonitoringService
class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
import com.stoutner.privacycell.services.RealtimeMonitoringService
class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
// Get the realtime monitoring preference. 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.
// Get the realtime monitoring preference. 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.
// Perform the functions according to the realtime monitoring status.
if (realtimeMonitoring) { // Realtime monitoring is enabled.
// Get a handle for the activity manager.
// Perform the functions according to the realtime monitoring status.
if (realtimeMonitoring) { // Realtime monitoring is enabled.
// Get a handle for the activity manager.
// Get a list of the running service info. The deprecated `getRunningServices()` now only returns services stared by Privacy Cell, but that is all we want to know anyway.
val runningServiceInfoList: List<ActivityManager.RunningServiceInfo> = activityManager.getRunningServices(1)
// Check to see if the service is currently running.
if (runningServiceInfoList.isEmpty()) { // The service is currently stopped.
// Get a list of the running service info. The deprecated `getRunningServices()` now only returns services stared by Privacy Cell, but that is all we want to know anyway.
val runningServiceInfoList: List<ActivityManager.RunningServiceInfo> = activityManager.getRunningServices(1)
// Check to see if the service is currently running.
if (runningServiceInfoList.isEmpty()) { // The service is currently stopped.
- // Start the service.
- context.startService(Intent(context, RealtimeMonitoringService::class.java))
+ // Start the service as a foreground service, which is required because the worker is running in the background.
+ applicationContext.startForegroundService(Intent(applicationContext, RealtimeMonitoringService::class.java))
} else { // The service is currently running.
// Create a service connection.
val serviceConnection = object : ServiceConnection {
} else { // The service is currently running.
// Create a service connection.
val serviceConnection = object : ServiceConnection {
- context.bindService(Intent(context, RealtimeMonitoringService::class.java), serviceConnection, 0)
+ applicationContext.bindService(Intent(applicationContext, RealtimeMonitoringService::class.java), serviceConnection, 0)