title: How to observe live data inside broadcast receiver
body: <p>My app should do an stuff every morning. So I am using broadcast receiver. When broadcast receiver is triggered I need to connect to my room-db. Get some data and do stuff with them. Normally I am using AndroidViewModel and LiveData for that. But I can not initiate a AndroidViewModel inside BroadcastReceiver. Instead I am initiating my repository and sending a read reaquest and assigning them to LiveData.</p>
  
  <p>Problem is I need to observe the live data. But Broadcast receiver is not an lifecycle-owner.</p>
  
  <pre><code>private void getData(){
      HomeRepository repository = HomeRepository.getInstance(context);
      alarms = repository.getAllAlarms();
      alarms.observe(this, new Observer&lt;List&lt;Alarm&gt;&gt;() {
          @Override
          public void onChanged(List&lt;Alarm&gt; alarmList) {
              //Do Stuff
          }
      });
  }
  </code></pre>
  
  <p>So it gives me a compile error saying Broadcast Reciever is not a lifecyle owner</p>
  
  <p>So I tried this:</p>
  
  <pre><code>public class MyBroadcastReceiver extends BroadcastReceiver implements LifecycleOwner 
  </code></pre>
  
  <p>It compiled but gave an error on the run time</p>
  
  <pre><code>Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.Lifecycle$State androidx.lifecycle.Lifecycle.getCurrentState()' on a null object reference
  </code></pre>
  
  <p>Can someone please show me the right direction to go here?</p>