title: cancel / resetting Alarm from OnAlarmReceiver does not seems to work
body: <p>I'm using the Lunch List example from CommonsWare to set and reset alarms.</p>
  
  <p>I set alarm at specific times and then if it's a recurring alarm, I try to reset the alarm to a new date/time.</p>
  
  <p>In OnAlarmReceiver I try to use the original three lines of code when first setting an alarm in my application's context (the activity). The three lines are:</p>
  
  <pre><code>ComponentName component=new ComponentName(context, OnBootReceiver.class);
  context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);   
  OnBootReceiver.setAlarm(context, itemId, mDateDue);
  </code></pre>
  
  <p>However this doesn't seem to work. What I tried then was adding this line:</p>
  
  <pre><code>OnBootReceiver.cancelAlarm(context, itemId);
  </code></pre>
  
  <p>But that also made no difference. I don't have a proper understanding of how this all ties together, but I suspect either:</p>
  
  <ol>
  <li>My context is wrong.</li>
  <li>I have to do something with the broadcast, like cancelling it as well.</li>
  <li>Perhaps there is a flag that needs to be changed?</li>
  </ol>
  
  <p>The idea is that every time a recurring alarm happens, it's reset by code. I know I can use repeating alarm but at this stage of my application I prefer to do this manually.</p>
  
  <p>Here is OnAlarmReceiver:</p>
  
  <pre><code>public class OnAlarmReceiver extends BroadcastReceiver {
  private static final int NOTIFY_ME_ID=1337;
  
  private static final String TAG = "OnAlarmReceiver";
  
  private DbAdapter mDbHelper;
  
  private String mDateDue;
  private String mFrequency;  
  
  @Override
  public void onReceive(Context context, Intent intent) {     
      mDbHelper = new DbAdapter(context);
      mDbHelper.open();
  
      AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
  
      Bundle bundle = intent.getExtras();
      long itemId = bundle.getLong("itemId");
  
      Cursor c  = mDbHelper.getItem(itemId);      
      String itemTitle = c.getString(c.getColumnIndex(Db.KEY_ITEMS_TITLE));
      int priority = c.getInt(c.getColumnIndex(Db.KEY_ITEMS_PRIORITY));
      long listId = c.getLong(c.getColumnIndex(Db.KEY_ITEMS_LIST_ID));
      String listTitle = mDbHelper.getListTitle(listId);
  
      mDateDue = c.getString(c.getColumnIndex(Db.KEY_ITEMS_DATE_DUE));
      mFrequency = c.getString(c.getColumnIndex(Db.KEY_ITEMS_FREQUENCY));
  
      Toast.makeText(context, "Due: " + listTitle + "-&gt;" + itemTitle, Toast.LENGTH_LONG).show();
  
      SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
      boolean useNotification=prefs.getBoolean("use_notification", true);
  
      // Check if the alarm must be reset to a new future date based on frequency
      checkResetAlarm(context, itemId);
  
      if (useNotification) {
          NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);                                  
          Notification notification = new Notification();         
          if (priority == 1) { // Display red icon
              notification=new Notification(R.drawable.nuvola_apps_kwrite, itemTitle, System.currentTimeMillis());    
          } else { // Display blue icon
              notification=new Notification(R.drawable.nuvola_apps_package_editors, itemTitle, System.currentTimeMillis());               
          }           
          Intent itemEditor = new Intent(context, ActivityEditItem.class);
          long lAlarmId = (long) (int) itemId;
          itemEditor.putExtra(DbAdapter.KEY_ITEMS_ITEM_ID, lAlarmId);
          itemEditor.putExtra("listId", listId);
          itemEditor.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          // For flags, also see http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
  
          PendingIntent i=PendingIntent.getActivity(context, 0, itemEditor, PendingIntent.FLAG_UPDATE_CURRENT);           
          notification.setLatestEventInfo(context, listTitle, itemTitle, i);          
          String notifyPreference = prefs.getString("notification_sound", "DEFAULT_RINGTONE_URI");                        
          notification.sound = Uri.parse(notifyPreference);
  
          int oldVolume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
  
          if (priority == 1) {
              Log.d(TAG, "A high priority item is due");
              //notification.defaults |= Notification.DEFAULT_VIBRATE;
              Vibrator v;
              v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
              v.vibrate(3000);
              int streamVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
              audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, streamVolume, 0);
          }
          mgr.notify((int) (long) itemId + NOTIFY_ME_ID, notification);
          audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, oldVolume, 0);
      }
      else {
          Intent i=new Intent(context, AlarmActivity.class);
          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(i);
      }
  }
  
  private void checkResetAlarm(Context context, long itemId) {        
      if (!mFrequency.equals("")) {           
          String newDateDue = Item.addMinutesToDate(mDateDue, mFrequency); 
          Log.d(TAG, "Due date " + mDateDue + " reset with frequency of " + mFrequency + ", new due date: " + newDateDue);
          mDbHelper.updateItemDueDate(itemId, newDateDue);
          Toast.makeText(context, "Resetting alarm...", Toast.LENGTH_LONG).show();
          ComponentName component=new ComponentName(context, OnBootReceiver.class);
          context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);   
          OnBootReceiver.cancelAlarm(context, itemId);
          OnBootReceiver.setAlarm(context, itemId, mDateDue);
      }       
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is OnBootReceiver:</p>
  
  <pre><code>public class OnBootReceiver extends BroadcastReceiver {
  
  public static void setAlarm(Context context, long itemId, String dateDue) {
  
      AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      Calendar cal=Calendar.getInstance();                
  
      String[] pieces=dateDue.split("/");
      String day_of_month = dateDue.substring(8,10);      
      String hour = dateDue.substring(11,13);     
      String minute = dateDue.substring(14,16);
      String second = dateDue.substring(17,19);
  
      cal.set(Calendar.YEAR, Integer.parseInt(pieces[0]));                
      cal.set(Calendar.MONTH, Integer.parseInt(pieces[1])-1);     
      cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day_of_month));
      cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
      cal.set(Calendar.MINUTE, Integer.parseInt(minute));
      cal.set(Calendar.SECOND, Integer.parseInt(second));     
      cal.set(Calendar.MILLISECOND, 0);
  
      if (cal.getTimeInMillis()&lt;System.currentTimeMillis()) {
          cal.add(Calendar.DAY_OF_YEAR, 1);
      }
  
      mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
              getPendingIntent(context, itemId));
  
  }
  
  /**
   * Cancel Alarm
   *  
   * @param ctxt
   * @param itemId
   */
  public static void cancelAlarm(Context ctxt, long itemId) {
      AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
      mgr.cancel(getPendingIntent(ctxt, itemId));
  }
  
  private static PendingIntent getPendingIntent(Context ctxt, long itemId) {
      //Intent i = new Intent(OnAlarmReceiver.ACTION, Uri.parse("timer:"+alarmId));
      Intent i=new Intent(ctxt, OnAlarmReceiver.class);
      i.putExtra("itemId", itemId);
      return(PendingIntent.getBroadcast(ctxt, (int) (long) itemId, i, 0));
  }
  
  // When the phone restarts all alarms must be reset by this method
  @Override
  public void onReceive(Context ctxt, Intent intent) {
      // To be added
      //setAlarm(ctxt);
  }
  </code></pre>
  
  <p>}     </p>