title: I want my service and broadcast receiver to run even if app is cleared from app list or forced stop
body: <p>I want my service and broadcast receiver to run in background even if app is closed or force stopped. </p>
  
  <p>My receiver is detecting screen on response and doing the task.
  Its working fine when app is open and when app is in app list but not closed.
  When i clear the app from app list, it is not detecting the screen on response.
  I have tried many ways but couldn't succeed. Help me out.</p>
  
  <p><strong>Manifest file :-</strong></p>
  
  <pre><code>&lt;receiver
          android:name=".broadcastReceiver.MyReceiver"
          android:enabled="true"
          android:exported="true"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.SCREEN_OFF"/&gt;
              &lt;action android:name="android.intent.action.SCREEN_ON"/&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;service
          android:name=".service.MyService"
          android:enabled="true"
          android:exported="true"
          android:stopWithTask="false"&gt;
  
      &lt;/service&gt;
  </code></pre>
  
  <p><strong>MyService class :-</strong></p>
  
  <pre><code>private var myReceiver:MyReceiver?=null
  
  private var defaultUEH:Thread.UncaughtExceptionHandler?=null
  private var uncaughtExceptionHandler = object:Thread.UncaughtExceptionHandler{
  
      override fun uncaughtException(thread: Thread?, ex: Throwable?) {
  
          Log.d("Exception found","Uncaught exception start!")
          ex!!.printStackTrace()
  
          unregisterReceiver(myReceiver)
  
          var intent = Intent(applicationContext, MyService::class.java)
          intent.setPackage(packageName)
          var pendingIntent = PendingIntent.getService(applicationContext,1,intent,PendingIntent.FLAG_ONE_SHOT)
          var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
          alarmManager.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+1000,pendingIntent)
          System.exit(2)
  
      }
  
  }
  
  override fun onBind(intent: Intent): IBinder? {
     return null
  }
  
  override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  
      var intentFilter = IntentFilter("android.intent.action.SCREEN_OFF")
      intentFilter.addAction("android.intent.action.SCREEN_ON")
  
      myReceiver = MyReceiver()
      registerReceiver(myReceiver , intentFilter)
  
      return START_STICKY
  }
  
  override fun onCreate() {
      super.onCreate()
      Log.d("Service" , "onCreate fun")
  }
  
  override fun onDestroy() {
  
      super.onDestroy()
  
      Log.d("Service" , "onDestroy fun")
  }
  
  override fun onTaskRemoved(rootIntent: Intent?) {
  
      unregisterReceiver(myReceiver)
  
      var intent = Intent(applicationContext, MyService::class.java)
      intent.setPackage(packageName)
      var pendingIntent = PendingIntent.getService(this,1,intent,PendingIntent.FLAG_ONE_SHOT)
      var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
      alarmManager.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+1000,pendingIntent)
      super.onTaskRemoved(rootIntent)
  
  }
  </code></pre>
  
  <p><strong>MyReceiver class:-</strong></p>
  
  <pre><code>override fun onReceive(context: Context?, intent: Intent?) {
  
      Log.d("Where ","Broadcast")
      if (intent!!.action.equals(Intent.ACTION_SCREEN_ON)){
  
          var dashboardActivity = DashboardActivity.instance
          var isSosEnabled = dashboardActivity!!.getSos()
  
          if (isSosEnabled){
              Log.d("Service Sos Enabled","True")
          }else{
              Log.d("Service Sos Enabled", "False")
          }
      }
  
  }
  </code></pre>