title: How schedule repeat alarm and one time alarm in android
body: <p>My app consists of two methods namely, <strong>one time alarm and repeat alarm for every day,</strong> i need to set alarm based on my requirement. When i set one time alarm it is working fine but when i set repeat alarm it is not working. I have worked on following method also but it did work for me.</p>
  
  <pre><code>Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(System.currentTimeMillis());
  calendar.set(Calendar.HOUR_OF_DAY, hour); // hour - user selected hour
  calendar.set(Calendar.MINUTE, minute);  // minute -user selected minute
  // setRepeating() lets you specify a precise custom interval--in this case,
  alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
      AlarmManager.INTERVAL_DAY, alarmIntent);
  </code></pre>
  
  <p>Here is the method of one time and repeat alarm in my fragment.</p>
  
  <pre><code> private SimpleDateFormat EEE_DD_MMM_YYYY_HH_MM_SS_A_Formatter = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss a"); //Tue, 07 Mar 2017 10:50:00 a.m.
  PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getBaseContext(), alarmId, intent, 0); // where alarmId is autoIncrement 
      AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
      try {
          Date alarmDate = EEE_DD_MMM_YYYY_HH_MM_SS_A_Formatter.parse(getDateTime()); // where we get both time and date from both pickers respectively.
          if (oneTimeAlarm) { 
          Logs.i(TAG,"one time alarm");
          alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getTime(), pendingIntent);
          Log.i(TAG,"one time alarm set!");
          } else {
              alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmDate.getTime(), AlarmManager.INTERVAL_DAY, pendingIntent);
              Log.i(TAG,"repeat alarm set!");
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>And here is my alarm receiver class file</p>
  
  <pre><code>public class AlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
  
      Vibrator vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);    //for Vibration
      vib.vibrate(2000);
   showNotification(context);
  }
  
  private void showNotification(Context context) {
      Intent intent = new Intent(context, MainActivity.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context,alarmId,intent, 0);
  
      //  NotificationCompat
      NotificationCompat.Builder mBuilder =
              new NotificationCompat.Builder(context)
                      .setSmallIcon(R.mipmap.ic_launcher)
                      .setContentTitle("Title")
                      .setContentText("text");
      mBuilder.setContentIntent(contentIntent);
      mBuilder.setDefaults(Notification.DEFAULT_SOUND);
      mBuilder.setAutoCancel(true);
      NotificationManager mNotificationManager =
              (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.notify(1, mBuilder.build());}}
  </code></pre>