title: Android Alarm being cancelled when shutting down the app. Leaked IntentReceiver
body: <p>I have a problem setting my alarm which is to send a notification about an upcoming event. I call it from my class with these lines of code (so far it starts 10 seconds after the app has been launched, just for testing purposes):</p>
  
  <pre><code>public void initNotifications(){
          AlarmManager alarms = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
  
          BroadcastManager receiver = new BroadcastManager();
          IntentFilter filter = new IntentFilter("ALARM_ACTION");
          registerReceiver(receiver, filter);
  
          Intent intent = new Intent("ALARM_ACTION");
          intent.putExtra("param", "My scheduled action");
          PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
          // I choose 3s after the launch of my application
          alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000, operation) ;
      }
  
  and here is the Broadcast Manager class (separate file):
  
  public class BroadcastManager extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          //Toast.makeText(context, intent.getStringExtra("param"),Toast.LENGTH_SHORT).show();
          Intent it =  new Intent(context, MainActivity.class);
          createNotification(context, it);
      }
  </code></pre>
  
  <p>I also has createNotification function, which works fine.</p>
  
  <p>Now I added the following line to android Manifest:</p>
  
  <pre><code>&lt;receiver
              android:name="BroadcastManager"
              android:label="BroadcastReceiverAux"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="ALARM" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  </code></pre>
  
  <p>What happens when I exit the app before ten seconds pass is the following error code:</p>
  
  <pre><code>Activity com.mysampleapp.MainMenu has leaked IntentReceiver com.mysampleapp.BroadcastManager@3370c300 that was originally registered here. Are you missing a call to unregisterReceiver()?
                                                                      android.app.IntentReceiverLeaked: Activity com.mysampleapp.MainMenu has leaked IntentReceiver com.mysampleapp.BroadcastManager@3370c300 that was originally registered here. Are you missing a call to unregisterReceiver()?
                                                                          at android.app.LoadedApk$ReceiverDispatcher.&lt;init&gt;(LoadedApk.java:960)
                                                                          at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:761)
                                                                          at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:2002)
                                                                          at android.app.ContextImpl.registerReceiver(ContextImpl.java:1982)
                                                                          at android.app.ContextImpl.registerReceiver(ContextImpl.java:1976)
                                                                          at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:503)
                                                                          at com.mysampleapp.MainMenu.initNotifications(MainMenu.java:672)
                                                                          at com.mysampleapp.MainMenu.onCreate(MainMenu.java:133)
                                                                          at android.app.Activity.performCreate(Activity.java:6283)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2756)
                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:145)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5940)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
  </code></pre>
  
  <p>I honestly looked through couple of topics here on Stack but I couldn't find the answer that would fix it. I want my notifications to run even after the reboot of the device, but to this I want to move later, after I manage to display them just after killing the app itself.</p>
  
  <p>Thank you in advance,
  John</p>