title: React Native Android - custom BroadcastReceiver/Service is being killed after some hours
body: <p>I'm building an app using React Native which requires a service that detects missed calls and sends that on the server and then shows a notification in phone status bar.</p>
  
  <p>I decided to write my own extension that will handle that because I didn't found any node module that will be sufficient for my needs. Unfortunately, service is being killed after some hours and I can't handle with that. Basically, I'm JavaScript developer and native code in Java is for me like a black hole so I'll be very grateful for any help.</p>
  
  <p>The app is using Headless JS for sending data to the server, basically all extension was based on articles:</p>
  
  <ul>
  <li><a href="http://www.learn-android-easily.com/2013/06/detect-missed-call-in-android.html" rel="nofollow noreferrer">http://www.learn-android-easily.com/2013/06/detect-missed-call-in-android.html</a></li>
  <li><a href="https://codeburst.io/simple-android-call-recorder-in-react-native-headlessjs-task-614bcc56efc4" rel="nofollow noreferrer">https://codeburst.io/simple-android-call-recorder-in-react-native-headlessjs-task-614bcc56efc4</a></li>
  </ul>
  
  <p>I've found some similar topics:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/23286780/android-service-process-being-killed-after-hours">Android service process being killed after hours</a></li>
  <li><a href="https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android" rel="nofollow noreferrer">https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android</a></li>
  </ul>
  
  <p>And tried to follow the instructions described there, but all of these solutions are related only with native code without using React Native and Headless JS so I don't know if that solutions will be ok for app that using React Native or probably (for sure) I'm doing something wrong.</p>
  
  <p>Here is my AndroidManifest part responsible for Service and BroadcastReceiver:</p>
  
  <pre><code>(...)
  &lt;service android:name="com.app.service.CallLogService" /&gt;
  &lt;receiver android:name="com.app.receiver.CallLogReceiver"&gt;
      &lt;intent-filter android:priority="0"&gt;
          &lt;action android:name="android.intent.action.PHONE_STATE" /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  (...)
  </code></pre>
  
  <p>My CallLogService class:</p>
  
  <pre><code>package com.app.service;
  import android.content.Intent;
  import android.os.Bundle;
  import com.facebook.react.HeadlessJsTaskService;
  import com.facebook.react.bridge.Arguments;
  import com.facebook.react.jstasks.HeadlessJsTaskConfig;
  import javax.annotation.Nullable;
  
  public class CallLogService extends HeadlessJsTaskService {
  
      @Nullable
      protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
  
          Bundle extras = intent.getExtras();
          return new HeadlessJsTaskConfig(
              "CallLog",
              extras != null ? Arguments.fromBundle(extras) : null,
              5000,
              true
          );
      }
  }
  </code></pre>
  
  <p>My CallLogReceiver class:</p>
  
  <pre><code>package com.app.receiver;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.util.Log;
  import com.app.service.CallLogService;
  import com.facebook.react.HeadlessJsTaskService;
  
  public final class CallLogReceiver extends BroadcastReceiver {
  
      public final void onReceive(Context context, Intent intent) {
  
          (...)
  
          callerPhoneNumber = intent.getStringExtra("incoming_number");
  
          Intent callIntent = new Intent(context, CallLogService.class);
  
          callIntent.putExtra("phone_number", callerPhoneNumber);
          context.startService(callIntent);
          HeadlessJsTaskService.acquireWakeLockNow(context);
  
      }
  }
  </code></pre>
  
  <p>I'm using React Native 0.50.3</p>
  
  <p>At the end I have an additional question. I noticed that after restart phone the service is also killed. How can I prevent such situation too?</p>
  
  <hr>
  
  <p><strong>Edit:</strong>
  I noticed that if app is in background then after sending request the response is not recorded.</p>
  
  <p>But after firing a new request the application is getting response from previous request. I'm using <code>axios</code> for doing <strong>ajax</strong>.</p>
  
  <p>Eg.</p>
  
  <pre><code>let callAjax = function(counter){
      console.log('Request ' + counter);
  
      axios.get('/user?ID=12345')
      .then(function (resp) {
          console.log('Response ' + counter);
      })
  };
  
  callAjax(1);
  setTimeout(() =&gt; {
      callAjax(2);
  }, 5000);
  </code></pre>
  
  <p>When app is in background then I have:</p>
  
  <p><code>Request 1</code></p>
  
  <p>After 5 sec</p>
  
  <p><code>Response 1 Request 2</code></p>
  
  <p>When app is in foreground then everything is ok:</p>
  
  <p><code>Request 1 Response 1</code></p>
  
  <p>After 5 sec</p>
  
  <p><code>Request 2 Response 2</code></p>