title: Run code every day at a specific time - AlarmManager
body: <p>Currently, I am trying to run a piece of code at a specific time. After some research, I think the correct way to go is to make usage of the <code>AlarmManger</code>. The code should be executed every day at 3 am. If the phone is at 3 am shut down, the code should be executed directly after turning the phone on.</p>
  
  <p>I used googled and found a lot of results. But my code isn't working correctly. </p>
  
  <p>Yesterday I got a notification. The notification time was 10 pm. But in the code, the time is set to 3 am. I set up a lot of alarm manager over the time (because of testing). Could it be possible, that the triggered AlarmManager was an old one? Note: Before setting up a new AlarmManager I deleted the complete application from my phone and installed it new. (I think this will delete all set AlarmManager's?)</p>
  
  <p>Okay, here is the code I am using:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(System.currentTimeMillis());
  calendar.add(Calendar.SECOND, 0);
  calendar.add(Calendar.MINUTE, 0);
  calendar.add(Calendar.HOUR, 3);
  
  Intent _myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
  _myIntent.putExtra("MyMessage","Content of the message");
  PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
  AlarmManager alarmManager = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, pendingIntent);</code></pre>
  </div>
  </div>
  </p>
  
  <p>This code will be executed on every startup of my app (inside <code>MainActivity's</code> <code>onCreate();</code> method). </p>