title: AlarmManager only lets one Service through when used with setRepeating in Android
body: <p>I've been struggling with this for a couple of days. What I want to do is run a service periodically, about 2-3 minutes apart. I have an Activity that is responsible for the interface and setting up the first alarm.</p>
  
  <p>The alarm is configured by a BroadcastReceiver which looks like this:</p>
  
  <pre><code>public class Receiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
            String message = "Alarm worked";
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            setAlarm(context);
      }
  
      public void setAlarm(Context context){
          AlarmManager am = (AlarmManager) context.
                                getSystemService(Context.ALARM_SERVICE);
          Intent intent = new Intent(context, Receiver.class);
          PendingIntent pi = PendingIntent.getBroadcast(context, 
                                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          Intent dailyUpdater = new Intent(context, DiscoveryService.class);
          context.startService(dailyUpdater);
          am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
                                + (1000 * 30) , pi);
      }
  }
  </code></pre>
  
  <p>I've tried using setRepeating for AlarmManager, but it still has the same effect. What happens is that the AlarmManager works how it should, it fires an Intent which the receiver gets and executes onReceive periodically, as it should. However, it executes the service only the first time. After the first time, the alarms still go off, but the service is not executed.</p>
  
  <p>I read some threads from people with similar problems, and one of them mentioned that PendingIntent lasts for only one send. Thus, I opted out to setting the alarm every time so I can set pendingIntent flag for updating every time.</p>
  
  <p>I tried making my service an intentService, which is fine, but then my bluetooth scanner inside the service does not work because intentService thread terminates without waiting for my bluetooth discovery to finish.</p>
  
  <p>Anyone have any idea what can help me?</p>
  
  <p>Here is part of my service:</p>
  
  <pre><code>public class DiscoveryService extends Service {
      public void onCreate() {
          super.onCreate();
          Toast.makeText(this, "MyAlarmService.onCreate()", 
                                   Toast.LENGTH_LONG).show();
          findEverything();
      }
  }
  </code></pre>
  
  <p>EDIT: This is the code that I currently have.</p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {
        String message = "Alarm worked";
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        Intent dailyUpdater = new Intent(context, DiscoveryService.class);
        context.startService(dailyUpdater);
  }
  
  public void setAlarm(Context context){
      // get a Calendar object with current time
      AlarmManager am=(AlarmManager)context. 
                               getSystemService(Context.ALARM_SERVICE);
      Intent intent = new Intent(context, Receiver.class);
      PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
                               PendingIntent.FLAG_UPDATE_CURRENT);
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 
                               (1000 * 30) , pi);
  }
  </code></pre>