I’m new on android programming and i’m working on android application and i’m stuck in creating multiple alarms.
my code only fires the last alarm and ignore the previous ones
the dates are stored in a database (SQlite) that the user specifies
this is my reminder manager class
public class ReminderManager {
private Context mContext;
private AlarmManager mAlarmManager;
public Appointments_ReminderManager(Context context) {
mContext = context;
mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}
public void setReminder(Long reminderId, Calendar when) {
Intent i = new Intent(mContext, Appointments_OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID_APPOIN, (long)reminderId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}
}
and this is the receiver class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ComponentInfo;
import android.util.Log;
public class OnAlarmReceiver extends BroadcastReceiver {
private static final String TAG = ComponentInfo.class.getCanonicalName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received wake up from alarm manager.");
long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID_APPOIN);
WakeReminderIntentService.acquireStaticLock(context);
Intent i = new Intent(context, ReminderService.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID_APPOIN, rowid);
context.startService(i);
}
}
can someone please help me modifying my code to fire multiple alarms specified by the user that is stored in a database.
Luckily the answer is pretty simple. Your code is:
The second parameter is the request_code is like an id for the PendingIntent and since you’re always override the previous PendingIntent. So what you have to do is to set a unique request code for every alarm you need. In your case you can use the reminderId, which is, I guess the DB row id. And because this is a unique ID for every Alarm it perfectly fits.