title: Alarm Manager with Recyclerview in android
body: <p>I want to call alarm in background with multiple <code>arrayList</code> items, in <code>recyclerview</code>. Alarm manager check in <code>arrayList(Database)</code> that when Date &amp; Time of item match with current Date &amp; Time, the alarm will be call and also send notification. This all work will be done in background.
  But in my application when I refresh activity then alarm is calling otherwise it do not call in background. kindly Help me.
  Thanks!</p>
  
  <p>This is My my Fragment Class where I call Alarm:</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
      v = inflater.inflate(R.layout.all_fragment,container,false);
      recyclerView = (RecyclerView) v.findViewById(R.id.recyclerviewAll);
  
      databaseHelper = new DatabaseHelper(getActivity());
  
      loadDatabase();
      setAlarm();
  
      return v;
  }
  
  public void loadDatabase() {
  
      LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
  
      databaseHelper.ShowAll(arrayList);
      adapter = new AllFragmentAdapter(getActivity(), arrayList);
      adapter.setOnTapListner(new onTapListener() {
          @Override
          public void OnTapView(int position) {
              Position = position;
           //   Toast.makeText(getContext(), "" + position, Toast.LENGTH_SHORT).show();
          }
      });
  
      recyclerView.setHasFixedSize(true);
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setItemAnimator(new DefaultItemAnimator());
      recyclerView.setAdapter(adapter);
      adapter.notifyDataSetChanged();
      adapter.notifyItemChanged(Position);
      layoutManager.supportsPredictiveItemAnimations();
  
  }
  
  
  public void setAlarm() {
      Calendar calendar1 = Calendar.getInstance();
      SimpleDateFormat format, format2;
      format2 = new SimpleDateFormat("dd-MM-yyyy HH:mm");
      String currentTime = format2.format(calendar1.getTime());
      String dbTimetoCompare = "";
      Date date1;
      long time1 = 0;
  
      if (arrayList.size() &gt; 0) {
          for (int i = 0; i &lt; arrayList.size(); i++) {
              if (arrayList.get(i).getSchedule().equals("Schedule")) {
                  String date = arrayList.get(i).getDate();
                  String time = arrayList.get(i).getTime();
                  Name = arrayList.get(i).getName();
                  Detail = arrayList.get(i).getDetail();
                  String datetime = date + " " + time;
                  format = new SimpleDateFormat("dd-MM-yyyy HH:mm");
                  try {
                      date1 = (Date) format.parse(datetime);
                      dbTimetoCompare = format.format(date1.getTime());
                      time1 = date1.getTime();
  
                      Calendar calendar = Calendar.getInstance();
                      calendar.setTimeInMillis(System.currentTimeMillis());
  
                      alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
                      Intent intent = new Intent(getContext(), MyAlarm.class);
                      intent.putExtra("CTime", currentTime);
                      intent.putExtra("DBTime", dbTimetoCompare);
                      pendingIntent = PendingIntent.getBroadcast(getActivity(), j, intent, 0);
  
                      if (currentTime.equals(dbTimetoCompare)){
  
                          for (j = 0; j &lt; 10; ++j) {
  
                              alarmManager.set(AlarmManager.RTC_WAKEUP, time1, pendingIntent);
  
                          }
                          Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
                          Ringtone r = RingtoneManager.getRingtone(getContext(), alert);
  
                          if (r == null) {
  
                              alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                              r = RingtoneManager.getRingtone(getContext(), alert);
  
                              if (r == null) {
                                  alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                                  r = RingtoneManager.getRingtone(getContext(), alert);
                              }
                          }
                          if (r != null)
                              r.play();
  
                        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext());
                          mBuilder.setSmallIcon(R.drawable.ic_notifications_black_24dp);
                          mBuilder.setContentTitle("Notification Alert");
                          mBuilder.setContentText("Name: " + Name + "  Detail: " + Detail);
                          NotificationManager manager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
                          manager.notify(j, mBuilder.build());
  
                          Toast.makeText(getContext(), "Alarm Called", Toast.LENGTH_SHORT).show();
                      }
                      if (alarmManager != null) {
                          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                                  1000 * 60 * 5, pendingIntent);
                          intentArray.add(pendingIntent);
                      }
  
                  } catch (ParseException e) {
                      Toast.makeText(getContext(), "" + e, Toast.LENGTH_SHORT).show();
                  }
              }
  
          }
      }
  }
  </code></pre>
  
  <p>This is my broadcast Class:</p>
  
  <pre><code>public class MyAlarm extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
  
      }
  }
  </code></pre>