Service dies even I created separate thread to the broadcast receiver which is inside the service

53 views Asked by At

I'm creating an App to monitoring call logs and send them to the cloud(Firebase Realtime database). I created Service to alive the app. Inside the service class, I created a Broadcast receiver to monitor the phone call states. I know about the background service restriction in android oreo and above

  1. First, I register the broadcast receiver like this

    @Override
     public void onCreate() {
         super.onCreate();
    
        IntentFilter filter = new IntentFilter();
        filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    
        registerReceiver(receiver, filter);
    
     }
    

After that, I can able to see call state changes as toast messages. but I try to save to the firebase realtime database inside the phone state listener class, My app background service stops. So I register the Broadcast receiver with a separate thread like below

@Override
   public void onCreate() {
      super.onCreate();

    IntentFilter filter = new IntentFilter();
    filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);

    HandlerThread handlerThread = new HandlerThread("ht");
    handlerThread.start();
    Looper looper = handlerThread.getLooper();
    Handler handler = new Handler(looper);
    registerReceiver(receiver, filter, null, handler);

}

Even though My background service stops. What went wrong here?

0

There are 0 answers