title: Android : How to open an activity with an alarm manager?
body: <p>I'm making an android app. The aim is to ring at a scheduled time and open an activity. I used several link from stackoverflow and other website about doing this but I got a problem : when I change the TimePicker's time, instead of setting the alarm, the alarm is set and the activity is open.</p>
  
  <p>I'd like to not open the activity when I change the time but only enable the alarm.</p>
  
  <p>Here is my code :</p>
  
  <p>Activity with the TimePicker :</p>
  
  <pre><code>tp_clock.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
  
              public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
  
                  int alarmHours = tp_clock.getCurrentHour();
                  int alarmMinutes = tp_clock.getCurrentMinute();
                  Long time = calculateTime(alarmHours, alarmMinutes);
  
                  Toast.makeText(context, "Alarm set", Toast.LENGTH_LONG).show();
                  alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(context, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
              }
          });    
  </code></pre>
  
  <p>calculateTime function :</p>
  
  <pre><code>public Long calculateTime(int alarmHours, int alarmMinutes)
      {
          Calendar c = Calendar.getInstance();
  
          int currentHours = c.get(Calendar.HOUR);
          if (c.get(Calendar.AM_PM) == 1)
              currentHours +=12;
          int currentMinutes = c.get(Calendar.MINUTE);
  
          int setAlarmHours = 0;
          int setAlarmMinutes = 0;
          int balanceWithMinute = 0;
  
          if (alarmMinutes &gt;= currentMinutes)
          {
              setAlarmMinutes = alarmMinutes - currentMinutes;
          }
          else
          {
              setAlarmMinutes = (60 - currentMinutes) + alarmMinutes;
              balanceWithMinute = 1;
          }
  
          if (alarmHours &gt; currentHours)
              setAlarmHours = alarmHours - currentHours;
  
          if (alarmHours &lt; currentHours)
              setAlarmHours = (24 - currentHours) +alarmHours - balanceWithMinute;
  
          if (alarmHours == currentHours &amp;&amp; balanceWithMinute == 1)
              setAlarmHours = 23;
  
          Long time = new GregorianCalendar().getTimeInMillis()+setAlarmHours*setAlarmMinutes*60*1000;
          return time;
      }
  </code></pre>
  
  <p>alarmReciever code :</p>
  
  <pre><code>public class AlarmReciever extends BroadcastReceiver
  {
      private String userToken;
      private String userId;
  
      @Override
      public void onReceive(Context context, Intent intent)
      {
          Intent i=new Intent(context, PlanASoundActivity.class);
          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          userToken = intent.getStringExtra(LoginActivity.USER_TOKEN);
          userId = intent.getStringExtra(LoginActivity.USER_ID);
  
          i.putExtra(LoginActivity.USER_TOKEN, userToken);
          i.putExtra(LoginActivity.USER_ID, userId);
          context.startActivity(i);
  
          Toast.makeText(context, "Alarm triggered", Toast.LENGTH_LONG).show();
  
      }
  </code></pre>
  
  <p>This function is the only one I defined in the AlarmReciever class.</p>