title: java.lang.IllegalArgumentException: Receiver not registered
body: <p>I have TrackingService component to track the location of the buses in my city based on Crowdsourcing. The TrackingService is operating in the background, then the data is transmitted to the server. I have an Map Activity to display the location of the buses, the user selected in the MainActivity( as Filter). </p>
  
  <p>The background TrackingService is started in the MainActivity  when the app launches.</p>
  
  <p>I notifing the <code>map</code> activity about the updated lcoation with the aid of the BroadcastReceiver as in the code below. The data is being retrieved to the  the map activity but I am facing problem to unregister my <code>bReceiver</code>.  I want to unregister the broadcast when the app goes in the background or when the user presses the back button but I am getting the error below:</p>
  
  <p>How can I fix it?</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>08-27 22:43:04.594: E/AndroidRuntime(19588): FATAL EXCEPTION: main
  08-27 22:43:04.594: E/AndroidRuntime(19588): Process: com.bustracker, PID: 19588
  08-27 22:43:04.594: E/AndroidRuntime(19588): java.lang.RuntimeException: Unable to stop activity {com.bustracker/com.bustracker.Map}: java.lang.IllegalArgumentException: Receiver not registered: com.bustracker.Map$1@2483d256
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4156)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4219)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.ActivityThread.access$1500(ActivityThread.java:177)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.os.Handler.dispatchMessage(Handler.java:102)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.os.Looper.loop(Looper.java:145)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.ActivityThread.main(ActivityThread.java:5944)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at java.lang.reflect.Method.invoke(Native Method)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at java.lang.reflect.Method.invoke(Method.java:372)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
  08-27 22:43:04.594: E/AndroidRuntime(19588): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.bustracker.Map$1@2483d256
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:822)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:2038)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:528)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at com.bustracker.Map.onStop(Map.java:418)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1275)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.Activity.performStop(Activity.java:6493)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4151)
  08-27 22:43:04.594: E/AndroidRuntime(19588):    ... 10 more
  </code></pre>
  
  <p><strong>TrackingService class:</strong></p>
  
  <pre><code>public class TrackingService extends Service implements
          LocationListener {
      public double pLong;
      public double pLat;
      ...
          @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          detectLocation();
          return START_STICKY;
      }
      private void detectLocation() {
          lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
                  this);
      }
      @Override
      public void onLocationChanged(Location location) {
  
          if (location != null) {
          pLong = location.getLongitude();
          pLat = location.getLatitude();
  
          Intent intent = new Intent(Map.RECEIVE_latLng);
          intent.putExtra("location",location);
          LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
             .....
  
       }  
  
  }
  </code></pre>
  
  <p><strong>Map activity:</strong></p>
  
  <pre><code>    public class Map extends FragmentActivity implements OnMapReadyCallback   {
     public static final String RECEIVE_latLng = "com.bustracker.RECEIVE_latLng";
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.map);
  
          LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
          IntentFilter intentFilter = new IntentFilter();
          intentFilter.addAction(RECEIVE_latLng);
          bManager.registerReceiver(bReceiver, intentFilter);
  
      }
  
  
      private BroadcastReceiver bReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              if(intent.getAction().equals(RECEIVE_latLng)) {
                                Location location = intent.getParcelableExtra("location");
               double lng = location.getLongitude();
               double lat = location.getLatitude();
               LatLng ll = new LatLng(lat, lng);
               MarkerOptions markerOpt = new MarkerOptions().title("My Location")
                          .position(ll);
               System.out.println("ABC map: "+ lat + " ; " + lng);
               myLocatMarker = map.addMarker(markerOpt);
              }
            }
          };      
        }
  @Override
  protected void onStop() {
      super.onStop();
      unregisterReceiver(bReceiver);      
  }
  </code></pre>