title: How to attach an AlarmManager to a BootReceiver
body: <p>I have the following in an activity, it basically sets an AlarmManager when the user logs into the app. The AlarmManager then periodically calls another activity that deletes transactions from the phone's DB. All this works fine.</p>
  
  <pre><code>// get a Calendar object with current time
  Calendar cal = Calendar.getInstance();
  // add 5 minutes to the calendar object
  cal.add(Calendar.MINUTE, 1);
  Intent intent = new Intent(EntryActivity.this, AlarmReceiver.class);
  intent.putExtra("alarm_message", "deleting transactions");
  // In reality, you would want to have a static variable for the request code instead of 192837
  PendingIntent sender = PendingIntent.getBroadcast(EntryActivity.this, 192837,
                                      intent, PendingIntent.FLAG_UPDATE_CURRENT);
  // Get the AlarmManager service
  AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
  am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender);
  </code></pre>
  
  <p>.</p>
  
  <pre><code>public class AlarmReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          try {
              Bundle bundle = intent.getExtras();
              String message = bundle.getString("alarm_message");
              Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
              Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
              myIntent.setAction("com.carefreegroup.startatboot.MyService");
              context.startService(myIntent);
          } catch (Exception e) {
              Toast.makeText(context, "There was an error somewhere, but we still"
                              + " received an alarm", Toast.LENGTH_SHORT).show();
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>public class SendOutstandingTransactions extends IntentService {
  
      private static final String TAG = SendOutstandingTransactions.class.getSimpleName();
      NfcScannerApplication nfcscannerapplication;
      Cursor c;
  
      @Override
      public void onCreate() {
          nfcscannerapplication = (NfcScannerApplication)getApplication();
          super.onCreate();
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
          nfcscannerapplication.loginValidate.deleteTableTransactions();
      }
  
      public SendOutstandingTransactions() {
          super("SendOutstandingTransactions");
      }
  }// end of class
  </code></pre>
  
  <p>The Service that deletes the transactions periodically is called only when the user first logs into app. It runs indefinitely from that point on. What if the user reboots the phone? The service will only resume the next time the user logs in.</p>
  
  <p>I have a BootReceiver but i don't know how to attach the AlarmManager. I've tried the following but ALARM_SERVICE cannot be resolved. Is this on the right lines?</p>
  
  <pre><code>public class MyBootReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          //  Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
          //  myIntent.setAction("com.carefreegroup.startatboot.MyService");
          //  context.startService(myIntent);
  
          // get a Calendar object with current time
          Calendar cal = Calendar.getInstance();
          // add 5 minutes to the calendar object
          cal.add(Calendar.MINUTE, 1);
          Intent intent = new Intent(MyBootReceiver.this, AlarmReceiver.class);
          intent.putExtra("alarm_message", "deleting transactions");
          // In reality, you would want to have a static variable for the request code instead of 192837
          PendingIntent sender = PendingIntent.getBroadcast(context, 192837,
                                      intent, PendingIntent.FLAG_UPDATE_CURRENT);
          // Get the AlarmManager service
          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
          //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
          am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender);
      }
  }
  </code></pre>