title: AlarmManager in Android
body: <p>I have code which is to be run <strong>daily</strong>; for this I'm trying to use <code>AlarmManager</code>. This is my code for triggering alarms:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      Intent i = new Intent(this, AlarmReciever.class);
      PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.cancel(pi); // cancel any existing alarms
      am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
          SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
          AlarmManager.INTERVAL_DAY, pi);
  }
  </code></pre>
  
  <p>This part of the code is calling <code>AlarmReciever</code> class as expected, but I want the code in the <code>AlarmReciever</code> class to be executed <strong>only</strong> once daily. It's being called multiple times. How do I restrict it?</p>
  
  <p>This is the <code>AlarmReciever</code> class:</p>
  
  <pre><code>public class AlarmReciever extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          System.out.println("in alarm reciever class");
  
      }
  } 
  </code></pre>
  
  <p>I'm trying to perform some business logic in the <code>onReceive()</code> method.</p>
  
  <p>In the manifest.xml file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;receiver android:name="com.xyz.reciever.AlarmReciever"&gt;&lt;/receiver&gt;
  
  &lt;uses-permission android:name="com.android.alarm.permission.SET_ALARM"/&gt;
  </code></pre>
  
  <p>are declared.</p>