title: Why would LocalBroadcastManager not work instead of Context.registerReceiver?
body: <p>I had to implement a feature to this app which consists of an <code>Activity</code> and a <code>Service</code> working on the background (it implements <code>Service</code>, not <code>IntentService</code>).</p>
  
  <p>I went through a few tutorials on the Internet that are supposed to work, and they all use <code>LocalBroadcastManager</code>, which by the way is the recommended by Android:</p>
  
  <blockquote>
    <p>If you don't need to send broadcasts across applications, consider
    using this class with <code>LocalBroadcastManager</code> instead of the more
    general facilities described below.</p>
  </blockquote>
  
  <p>I literally lost a day to find out the problem why it wouldn't work for me: it only works if I use <code>Context.sendBroadcast()</code>. and <code>Context.registerReceiver()</code> instead of the <code>LocalBroadcastManager</code> methods.</p>
  
  <p>Now my app is working, but I feel I am going against the best practices, and I don't know why. 
  Any ideas why it could be happening?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After I wrote this question I went further on the problem. <code>LocalBroadcastManager</code> works through a Singleton, as we should call <code>LocalBroadcastManager.getInstance(this).method()</code>. I logged both instances (in the <code>Activity</code> and in the <code>Service</code>) and they have different memory addresses. 
  Now I came to another question, shouldn't a <code>Service</code> have the same <code>Context</code> as the <code>Activity</code> that called it? From <a href="http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html" rel="nofollow noreferrer">this article</a> a Service runs on the Main Thread, hence I'd think the <code>Context</code> would be
  the same. </p>
  
  <p>Any thoughts on that? (sorry for the long post)</p>
  
  <p><strong>Code samples:</strong></p>
  
  <p><strong>MyService</strong></p>
  
  <pre><code>public class MyService extends Service {
  
  ...
  
  // When an event is triggered, sends a broadcast
  
  Intent myIntent = new Intent(MainActivity.MY_INTENT);
  myIntent.putExtra("myMsg","msg");
  sendBroadcast(myIntent);
  
  // Previously I was trying:
  // LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(myIntent);
  
  }
  </code></pre>
  
  <p><strong>MyActivity</strong></p>
  
  <pre><code>public class MainActivity {
  
  ...
  
  private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) { 
              Log.d("onReceive", "received!");
              // TODO something
          }
      };
  
  @Override
  protected void onResume() {
      super.onResume();
      registerReceiver(messageReceiver, new IntentFilter(MY_INTENT));
      // Previously I was trying:
      // LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(messageReceiver, new IntentFilter(MY_INTENT));
  }
  }
  </code></pre>