How do I make every call to my alarmManager unique?
I'm having problem in scheduling my alarm. I want to make every call of my alarm unique so that it will not overlap the previous alarm that I've already set. This is my code:
public void compareDates()
{
String callName;
String dateStart;
String dateDue;
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
long callTime = System.currentTimeMillis();
Date callsDateStart = new Date();
Date dateNow = new Date();
GlucoseDatabaseAdapter gda = new GlucoseDatabaseAdapter(this);
gda.open();
Cursor c = gda.getEntries(GlucoseDatabaseAdapter.TABLE_CALLS, null,null,null,null,null,null);
AlarmManager callsAlarm = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this,callsNotify.class);
callAlarm = PendingIntent.getService(this, 0, alarmIntent, 0);
if(c.moveToFirst())
{
do
{
//GET DATA
callName = c.getString(GlucoseDatabaseAdapter.CALLS_NAME_KEY);
dateStart = c.getString(GlucoseDatabaseAdapter.CALLS_DATE_START_KEY);
dateDue = c.getString(GlucoseDatabaseAdapter.CALLS_DATE_DUE_KEY);
//COMPARE DATES
try
{
callsDateStart = sdf1.parse(dateStart); } 开发者_StackOverflow中文版
catch (ParseException e)
{ // TODO Auto-generated catch block
e.printStackTrace(); }
if(callsDateStart.after(dateNow))
{
long callsDs = callsDateStart.getTime();
long ff = callsDs - callTime;
callsAlarm.set(AlarmManager.RTC, System.currentTimeMillis() + ff, callAlarm);
}
}while(c.moveToNext()); }
// I'm calling callsAlarm multiple times in this code. When I set callsAlarm here it only sets the latest one. How do I make every set here unique?
You have to make sure the Intent you are passing in is unique or can only be called once. It looks like you are using the same one over and over so it is getting over written. You may want to change your pending intent to look like something as follows and read the android documentation for other possible flags to set
PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent, PendingIntent.FLAG_ONE_SHOT);
精彩评论