title: Crash accessing RoomDatabase from BackgroundService or JobScheduler
body: <p>I am trying to access the <code>RoomDatabase</code> from the context of <code>Service</code>/<code>IntentService</code> extended background service class and from JobScheduler based JobService extended class.</p>
  
  <p>Since both are service class, so both are itself are context. But RoomDatabase is not getting initialized by that context. And the <code>getApplicationContext()</code> of these classes is also null.</p>
  
  <p>The requirement is, when the application is in background (killed it's process from <code>multi-tasking</code> also), then Network based or Location based background service triggers will wants to access <code>RoomDatabase</code> and causing it to crash.</p>
  
  <pre><code>Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
          at android.content.ContextWrapper.getSystemService(ContextWrapper.java:714)
          at android.arch.persistence.room.RoomDatabase$JournalMode.resolve(RoomDatabase.java:388)
          at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:628)
  </code></pre>
  
  <p>One example of implementation is :</p>
  
  <pre><code>internal class JobSchedulerService:JobService() {
  
      private lateinit var logger:Logger
      private lateinit var appContext:Context
  
      override fun onCreate() {
          this.appContext = this@JobSchedulerService
  
          super.onCreate()
      }
  
      override fun onStartJob(params: JobParameters?): Boolean {
          Log.w("TAG","JobSchedulerService: onStartJob: $params context:$this")
  
          logger = Logger(this.appContext)
          logger.log(logInfo = "JobSchedulerService: onStartJob: $params")
  
          if (params!!.jobId == EVENT_TYPE.LOCATION_CHANGE.value) {
              val intent = Intent(this, BackgroundService::class.java)
              intent.putExtra("JobSchedulerService: eventType", EVENT_TYPE.LOCATION_CHANGE.value)
              // this will call BackgroundService onHandle fun
              startService(intent)
          }
          return true
      }
  
      override fun onStopJob(params: JobParameters?): Boolean {
  
          logger = Logger(this.appContext)
          logger.log(logInfo = "JobSchedulerService: onStopJob: $params")
          return true
      }
  
      override fun onLowMemory() {
  
          logger = Logger(this.appContext)
          logger.log(logInfo = "JobSchedulerService: low memory trigged")
          super.onLowMemory()
      }
  }
  </code></pre>
  
  <p>another service class example is:</p>
  
  <pre><code>class NetworkJobService:JobService() {
  
      private lateinit var logger: Logger
      private lateinit var appContext: Context
      private val connectivityManager:ConnectivityManager by lazy { this.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager }
  
      private val wifiManager by lazy {
          this@NetworkJobService.getSystemService(Context.WIFI_SERVICE) as WifiManager
      }
      private val networkCallback: ConnectivityManager.NetworkCallback by lazy {
          object : ConnectivityManager.NetworkCallback() {
              override fun onCapabilitiesChanged(network: Network?, networkCapabilities: NetworkCapabilities?) {
                  super.onCapabilitiesChanged(network, networkCapabilities)
  
                  val intent = Intent(this@NetworkJobService, BackgroundService::class.java)
                  intent.putExtra("eventType", EVENT_TYPE.NETWORK_CHANGE.value)
                  // this will call BackgroundService onHandle fun
                  this@NetworkJobService.startService(intent)
  
              }
  
              override fun onLinkPropertiesChanged(network: Network?, linkProperties: LinkProperties?) {
                  super.onLinkPropertiesChanged(network, linkProperties)
  
                  val intent = Intent(this@NetworkJobService, BackgroundService::class.java)
                  intent.putExtra("eventType", EVENT_TYPE.NETWORK_CHANGE.value)
                  // this will call BackgroundService onHandle fun
                  this@NetworkJobService.startService(intent)
  
              }
  
              override fun onLost(network: Network?) {
                  super.onLost(network)
  
                  val intent = Intent(this@NetworkJobService, BackgroundService::class.java)
                  intent.putExtra("eventType", EVENT_TYPE.NETWORK_CHANGE.value)
                  // this will call BackgroundService onHandle fun
                  this@NetworkJobService.startService(intent)
  
              }
  
              override fun onUnavailable() {
                  super.onUnavailable()
  
                  val intent = Intent(this@NetworkJobService, BackgroundService::class.java)
                  intent.putExtra("eventType", EVENT_TYPE.NETWORK_CHANGE.value)
                  // this will call BackgroundService onHandle fun
                  this@NetworkJobService.startService(intent)
  
              }
  
              override fun onLosing(network: Network?, maxMsToLive: Int) {
                  super.onLosing(network, maxMsToLive)
  
                  val intent = Intent(this@NetworkJobService, BackgroundService::class.java)
                  intent.putExtra("eventType", EVENT_TYPE.NETWORK_CHANGE.value)
                  // this will call BackgroundService onHandle fun
                  this@NetworkJobService.startService(intent)
  
              }
          }
      }
  
      override fun onCreate() {
          this.appContext = this@NetworkJobService
          super.onCreate()
      }
      override fun onStartJob(params: JobParameters?): Boolean {
          logger = Logger(this.appContext)
          logger.log(logInfo = "NetworkJobService: onStartJob: $params")
  
          if (params!!.jobId == EVENT_TYPE.NETWORK_CHANGE.value){
              val intent = Intent(this.appContext, BackgroundService::class.java)
              intent.putExtra("eventType", EVENT_TYPE.NETWORK_CHANGE.value)
              // this will call BackgroundService onHandle fun
              startService(intent)
          }
          connectivityManager.registerNetworkCallback(NetworkRequest.Builder().build(), networkCallback)
          return true
      }
  
      override fun onStopJob(params: JobParameters?): Boolean {
          logger = Logger(this.appContext)
          logger.log(logInfo = "NetworkJobService: onStopJob: $params")
          return true
      }
  
      override fun onLowMemory() {
          logger = Logger(this.appContext)
          logger.log(logInfo = "NetworkJobService: low memory trigged")
          super.onLowMemory()
      }
  }
  </code></pre>
  
  <p>And then Logger is having singleton db instance</p>
  
  <pre><code>internal class Logger {
  
      internal var appContext: Context
  
      internal var database: MobileSecurityDatabase
  
      internal constructor(context:Context) {
          appContext = context
          database = MobileSecurityDatabase.getInstance(appContext)
      }
  
  
      fun log(logInfo:String){
  
          runBlocking {
              launch(Dispatchers.Default){
                  val logTime = Date().time
                  val cipher:Cipher = Utils.generateCipher(logTime)
                  val sealedObject = SealedObject(appId + " : " + logInfo, cipher)
  
                  Log.w("logger", appId!! + " : " + logInfo)
                  val byteArray = Utils.sealedObjectStream(sealedObject)
                  if (byteArray != null)  {
                      val logData = LogData(logTime, byteArray)
                      database.logDao().insertLog(logData = logData)
                  }
              }
          }
  
      }
  }
  </code></pre>
  
  <p>In last comes RoomPersistence class which is singleton:</p>
  
  <pre><code>@Database(entities = [LogData::class, UserData::class], version = 1, exportSchema = false)
  abstract class MobileSecurityDatabase : RoomDatabase() {
  
      abstract fun logDao(): LogDao
  
      abstract fun userDao(): UserDao
  
  
      companion object : SingletonHolder&lt;MobileSecurityDatabase, Context&gt;({ it: Context -&gt;
  
          Room.databaseBuilder(it, MobileSecurityDatabase::class.java, DB_NAME)
                  .addCallback(object: RoomDatabase.Callback() {
                      override fun onCreate(db: SupportSQLiteDatabase){                       
                      }
  
                      override fun onOpen(db: SupportSQLiteDatabase) {
                          super.onOpen(db)
                      }
                  })
                  .build()
      })
  }
  </code></pre>
  
  <p>After making the changes suggested in answers for scope of context. I have started getting exception when my app is closed and in locked state. I unlocked the app and launched some other app.</p>
  
  <pre><code>10-06 22:20:00.086 886-886/? D/SFPerfTracer:        layers: (4:12) (StrictModeFlash#0 (0xadcc8000): 0:146)* (StatusBar#0 (0xade3c000): 3:69585) (com.android.systemui.ImageWallpaper#0 (0xade45000): 0:3760) (DimLayerController/Stack=0#0 (0xadb1e000): 0:867)* (Sprite#0 (0xade43000): 0:12)* (animation background stackId=1#0 (0xadb3b000): 0:91)* (NavigationBar#0 (0xadb34000): 0:2770) (Splash Screen android.thakur.com.mobilesecurityapplication#0 (0xadbda000): 0:16)- (android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurityapplication.MainActivity#0 (0xadb31000): 0:29)- (com.motorola.launcher3/com.android.launcher3.CustomizationPanelLauncher#0 (0xadbda000): 0:28)- (com.motorola.launcher3/com.android.launcher3.CustomizationPanelLauncher#1 (0xafedd000): 0:16)- (com.android.systemui/com.android.systemui.recents.RecentsActivity#0 (0xadb31000): 25:62) 
  10-06 22:29:50.104 886-886/? D/SFPerfTracer:        layers: (3:11) (StrictModeFlash#0 (0xadcc8000): 0:146)* (StatusBar#0 (0xade3c000): 0:69860) (com.android.systemui.ImageWallpaper#0 (0xade45000): 0:3761)* (DimLayerController/Stack=0#0 (0xadb1e000): 0:867)* (Sprite#0 (0xade43000): 0:12)* (animation background stackId=1#0 (0xadb3b000): 0:93)* (NavigationBar#0 (0xadb34000): 12:2824) (com.motorola.launcher3/com.android.launcher3.CustomizationPanelLauncher#0 (0xafedd000): 0:26)* (com.motorola.launcher3/com.android.launcher3.CustomizationPanelLauncher#1 (0xadbda000): 0:58)* (Splash Screen android.thakur.com.mobilesecurityapplication#0 (0xadb72000): 2:27) (android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurityapplication.MainActivity#0 (0xad606000): 3:3)* 
  10-06 22:29:59.630 886-886/? D/SFPerfTracer:        layers: (4:13) (StrictModeFlash#0 (0xadcc8000): 0:146)* (StatusBar#0 (0xade3c000): 1:69867) (com.android.systemui.ImageWallpaper#0 (0xade45000): 0:3762) (DimLayerController/Stack=0#0 (0xadb1e000): 0:867)* (Sprite#0 (0xade43000): 0:12)* (animation background stackId=1#0 (0xadb3b000): 0:93)* (NavigationBar#0 (0xadb34000): 2:2894) (com.motorola.launcher3/com.android.launcher3.CustomizationPanelLauncher#0 (0xafedd000): 0:26)- (com.motorola.launcher3/com.android.launcher3.CustomizationPanelLauncher#1 (0xadbda000): 0:58)- (Splash Screen android.thakur.com.mobilesecurityapplication#0 (0xadb72000): 0:37)- (android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurityapplication.MainActivity#0 (0xad606000): 0:27)- (com.android.systemui/com.android.systemui.recents.RecentsActivity#0 (0xadb72000): 35:47) (thumbnail anim#0 (0xafedd000): 0:23)- 
  10-06 22:40:32.708 29528-29528/? E/AndroidRuntime: FATAL EXCEPTION: main
      Process: android.thakur.com.mobilesecurityapplication, PID: 29528
      java.lang.RuntimeException: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{aecac5d u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
          ...
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
       Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{aecac5d u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1536)
          at android.app.ContextImpl.startService(ContextImpl.java:1492)
          at android.content.ContextWrapper.startService(ContextWrapper.java:650)
          at android.thakur.com.mobilesecurity.services.jobs.NetworkJobService.onStartJob(NetworkJobService.kt:90)
          at android.app.job.JobService$1.onStartJob(JobService.java:71)
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
          ...
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 
  10-06 22:41:08.215 29930-29930/? E/AndroidRuntime: FATAL EXCEPTION: main
      Process: android.thakur.com.mobilesecurityapplication, PID: 29930
      java.lang.RuntimeException: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{6b2ae65 u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6626)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
       Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{6b2ae65 u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1536)
          at android.app.ContextImpl.startService(ContextImpl.java:1492)
          at android.content.ContextWrapper.startService(ContextWrapper.java:650)
          at android.thakur.com.mobilesecurity.services.jobs.NetworkJobService.onStartJob(NetworkJobService.kt:90)
          at android.app.job.JobService$1.onStartJob(JobService.java:71)
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:164) 
          at android.app.ActivityThread.main(ActivityThread.java:6626) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 
  10-06 22:42:08.919 30314-30314/? E/AndroidRuntime: FATAL EXCEPTION: main
      Process: android.thakur.com.mobilesecurityapplication, PID: 30314
      java.lang.RuntimeException: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{a8df8a3 u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6626)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
       Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{a8df8a3 u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1536)
          at android.app.ContextImpl.startService(ContextImpl.java:1492)
          at android.content.ContextWrapper.startService(ContextWrapper.java:650)
          at android.thakur.com.mobilesecurity.services.jobs.NetworkJobService.onStartJob(NetworkJobService.kt:90)
          at android.app.job.JobService$1.onStartJob(JobService.java:71)
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
          ...
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 
  10-06 22:42:08.951 1768-3051/? W/ActivityManager: Scheduling restart of crashed service android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.jobs.JobSchedulerService in 1000ms
      Scheduling restart of crashed service android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.jobs.NetworkJobService in 11000ms
  10-06 22:45:07.604 1768-1768/? I/ActivityManager: Start proc 30522:android.thakur.com.mobilesecurityapplication/u0a273 for service android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.jobs.NetworkJobService
  10-06 22:45:08.200 1768-4078/? W/ActivityManager: Background start not allowed: service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) } to android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService from pid=30522 uid=10273 pkg=android.thakur.com.mobilesecurityapplication
  10-06 22:45:08.205 30522-30522/? E/AndroidRuntime: FATAL EXCEPTION: main
      Process: android.thakur.com.mobilesecurityapplication, PID: 30522
      java.lang.RuntimeException: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{ed0b59a u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
          ...
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
       Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{ed0b59a u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          ....
          at android.thakur.com.mobilesecurity.services.jobs.NetworkJobService.onStartJob(NetworkJobService.kt:90)
          at android.app.job.JobService$1.onStartJob(JobService.java:71)
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
          ...
  10-06 22:45:08.241 1768-4082/? I/ActivityManager: Process android.thakur.com.mobilesecurityapplication (pid 30522) has died: vis  TRN
  10-06 22:45:49.314 1768-1930/? I/ActivityManager:   Force stopping service ServiceRecord{35e64ad u0 android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService}
  </code></pre>
  
  <p>And then got crash pop up on my device.
   The exception at that time was:</p>
  
  <pre><code>10-06 22:46:18.260 1768-2052/? I/LaunchCheckinHandler: Displayed android.thakur.com.mobilesecurityapplication/.MainActivity,cp,ca,27875
  10-06 22:46:20.228 886-886/? D/SFPerfTracer:        layers: (4:11) (StrictModeFlash#0 (0xadcc8000): 0:149)* (StatusBar#0 (0xade3c000): 2:70759) (com.android.systemui.ImageWallpaper#0 (0xade45000): 0:3771) (DimLayerController/Stack=0#0 (0xadb1e000): 0:867)* (Sprite#0 (0xade43000): 0:12)* (animation background stackId=1#0 (0xadb3b000): 0:109)* (NavigationBar#0 (0xadb34000): 4:3666) (AOD#0 (0xade21000): 0:81)- (android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurityapplication.MainActivity#0 (0xadbd5000): 0:30)- (com.android.systemui/com.android.systemui.recents.RecentsActivity#0 (0xafedd000): 24:35) (thumbnail anim#0 (0xade21000): 0:22)- 
  10-06 22:46:20.559 30861-30861/? E/ActivityThread: Activity android.thakur.com.mobilesecurityapplication.MainActivity has leaked IntentReceiver android.thakur.com.mobilesecurity.services.jobs.BatteryReciever@f325fba that was originally registered here. Are you missing a call to unregisterReceiver()?
      android.app.IntentReceiverLeaked: Activity android.thakur.com.mobilesecurityapplication.MainActivity has leaked IntentReceiver android.thakur.com.mobilesecurity.services.jobs.BatteryReciever@f325fba that was originally registered here. Are you missing a call to unregisterReceiver()?
          at android.app.LoadedApk$ReceiverDispatcher.&lt;init&gt;(LoadedApk.java:1360)
          at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1141)
          at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1436)
          at android.app.ContextImpl.registerReceiver(ContextImpl.java:1397)
          at android.app.ContextImpl.registerReceiver(ContextImpl.java:1385)
          at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:609)
          at android.thakur.com.mobilesecurity.services.Services.startJob(Services.kt:72)
          at android.thakur.com.mobilesecurity.services.backgroundservices.LocationBackgroundService.startLocationChange(LocationBackgroundService.kt:126)
          at android.thakur.com.mobilesecurity.services.backgroundservices.LocationBackgroundService$onCreate$1.onLocationChanged(LocationBackgroundService.kt:46)
          at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:292)
          ...
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
  10-06 22:46:20.573 1768-2529/? I/ActivityManager: Killing 30861:android.thakur.com.mobilesecurityapplication/u0a273 (adj 1001): remove task
  10-06 22:46:20.600 1768-3054/? W/ActivityManager: Scheduling restart of crashed service android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.LocationBackgroundService in 1000ms
  10-06 22:46:21.610 1768-1930/? I/ActivityManager: Start proc 31887:android.thakur.com.mobilesecurityapplication/u0a273 for service android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.LocationBackgroundService
  10-06 22:46:21.673 1768-3492/? W/ActivityManager: Stopping service due to app idle: u0a273 -29s581ms android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.LocationBackgroundService
  10-06 22:46:22.057 1768-4081/? W/ActivityManager: Service done with onDestroy, but executeNesting=2: ServiceRecord{e29b4af u0 android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.LocationBackgroundService}
  10-06 22:49:37.973 1768-2376/? I/ActivityManager: Killing 31887:android.thakur.com.mobilesecurityapplication/u0a273 (adj 906): empty #17
  10-06 22:59:21.333 1768-1768/? I/ActivityManager: Start proc 4425:android.thakur.com.mobilesecurityapplication/u0a273 for service android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.jobs.JobSchedulerService
  10-06 22:59:21.791 4425-4425/android.thakur.com.mobilesecurityapplication I/InstantRun: starting instant run server: is main process
  10-06 22:59:21.825 4425-4425/android.thakur.com.mobilesecurityapplication W/TAG: JobSchedulerService: onStartJob: android.app.job.JobParameters@6c9edb4 context:android.thakur.com.mobilesecurity.services.jobs.JobSchedulerService@79f3ddd
  10-06 22:59:21.890 4425-4467/android.thakur.com.mobilesecurityapplication W/logger: unknown : JobSchedulerService: onStartJob: android.app.job.JobParameters@6c9edb4
  10-06 22:59:21.943 4425-4467/android.thakur.com.mobilesecurityapplication W/logger: unknown : NetworkJobService: onStartJob: android.app.job.JobParameters@8172c49
  10-06 22:59:21.950 1768-3501/? W/ActivityManager: Background start not allowed: service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) } to android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService from pid=4425 uid=10273 pkg=android.thakur.com.mobilesecurityapplication
  10-06 22:59:21.951 4425-4425/android.thakur.com.mobilesecurityapplication E/JobServiceEngine: Error while executing job: 4002
  10-06 22:59:21.951 4425-4425/android.thakur.com.mobilesecurityapplication D/AndroidRuntime: Shutting down VM
  10-06 22:59:21.955 4425-4425/android.thakur.com.mobilesecurityapplication E/AndroidRuntime: FATAL EXCEPTION: main
      Process: android.thakur.com.mobilesecurityapplication, PID: 4425
      java.lang.RuntimeException: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{9874a04 u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6626)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
       Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.backgroundservices.BackgroundService (has extras) }: app is in background uid UidRecord{9874a04 u0a273 TRNB idle change:uncached procs:1 seq(0,0,0)}
          at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1536)
          at android.app.ContextImpl.startService(ContextImpl.java:1492)
          at android.content.ContextWrapper.startService(ContextWrapper.java:650)
          at android.thakur.com.mobilesecurity.services.jobs.NetworkJobService.onStartJob(NetworkJobService.kt:90)
          at android.app.job.JobService$1.onStartJob(JobService.java:71)
          at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:164) 
          at android.app.ActivityThread.main(ActivityThread.java:6626) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 
  10-06 22:59:21.969 1768-1931/? I/ActivityManager: Showing crash dialog for package android.thakur.com.mobilesecurityapplication u0
  10-06 22:59:29.832 1768-1768/? W/JobServiceContext: No response from client for onStartJob c8cbe13 #u0a273/4002 android.thakur.com.mobilesecurityapplication/android.thakur.com.mobilesecurity.services.jobs.NetworkJobService
  10-06 22:59:30.962 886-886/? D/SFPerfTracer:        layers: (6:10) (StrictModeFlash#0 (0xadcc8000): 0:151)* (StatusBar#0 (0xade3c000): 1:71776) (com.android.systemui.ImageWallpaper#0 (0xade45000): 0:3779) (DimLayerController/Stack=0#0 (0xadb1e000): 0:877) (Sprite#0 (0xade43000): 0:12)* (animation background stackId=1#0 (0xadb3b000): 0:109)* (NavigationBar#0 (0xadb34000): 0:3745) (com.motorola.launcher3/com.android.launcher3.CustomizationPanelLauncher#0 (0xafedd000): 0:16)* (com.motorola.launcher3/com.android.launcher3.CustomizationPanelLauncher#1 (0xadb31000): 0:46) (Application Error: android.thakur.com.mobilesecurityapplication#0 (0xadb28000): 0:14) 
  </code></pre>