[Java for Android App Development] simone.bellandi.dev:
Good morning people,

I decided to ask here advice about an issue.

I'm working with AlarmManagers and a BroadcastReceiver, and I want to set 2(or more) alarms using a single BroadcastReceiver within a function.

My problem is that only the earliest alarm is received by the BroadcastReceiver at the right time, while the following alarm is received more than 5 minutes late or not at all.

I tried a lot of configurations, but I can't find where I'm wrong. Note that I'm using different request codes for my PendingIntents.

I should specify that the second alarm could be set a minute later than the 1st, which is my case. It seems that the BroadcastReceiver can't handle the second alarm, something like it's busy.

I can't try the code on a real android device now, so I'm using the emulator with Android Studio, so I don't really know whether the issue is caused by the emulator or not.

P.S. From my last tests it seems the second alarm triggers only at certain times. I can set it at every hour and minute I want, but it only starts at HH:00, HH:15, HH:30, HH:45, and so on O_O.

Please, it's driving me mad.

Thanks in advance for help.

Activity code:

public class mainActivity extends Activity {

final static int START = 0, STOP = 1;

AlarmManager alarmManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void doneButtonClick(View view) {
int startHour = Integer.parseInt(((EditText)findViewById(R.id.startHour)).getText().toString());
int startMinute = Integer.parseInt(((EditText)findViewById(R.id.startMin)).getText().toString());
int stopHour = Integer.parseInt(((EditText)findViewById(R.id.stopHour)).getText().toString());
int stopMinute = Integer.parseInt(((EditText)findViewById(R.id.stopMin)).getText().toString());

alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
setAlarm(START, startHour, startMinute);
setAlarm(STOP, stopHour, stopMinute);
}

private void setAlarm(int type, int hour, int minute) {
Intent intent = new Intent(this, MyReceiver.class);
intent.putExtra("type", type);
PendingIntent pIntent = PendingIntent.getBroadcast(this, type, intent, PendingIntent.FLAG_NO_CREATE);
if (pIntent != null) {
Log.d("alarm", "existing");
alarmManager.cancel(pIntent);
}
pIntent = PendingIntent.getBroadcast(this, type, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.HOUR_OF_DAY, hour);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
}
}

BroadcastReceiver code:

public class MyReceiver extends BroadcastReceiver {

final static int START = 0, STOP = 1;

public MyReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
int type = intent.getExtras().getInt("type");
Log.d("alarm", "received");
switch(type) {
case START:
Log.d(String.valueOf(START), "start");
break;
case STOP:
Log.d(String.valueOf(STOP), "stop");
break;
}
}
}

Android Manifest:

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".mainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:name=".MyReceiver">

</receiver>
</application>

Results: