如何对alarmManager的每次调用都是唯一的?

时间:2011-02-26 02:27:03

标签: android scheduling alarmmanager

我在安排闹钟时遇到问题。我希望每次调用我的闹钟都是独一无二的,这样它就不会重叠我之前已经设置过的警报。 这是我的代码:

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); }  
      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());  }  

//我在这段代码中多次调用callsAlarm。当我在这里设置callsAlarm时,它只设置最新的一个。我如何让每一套都独一无二?

1 个答案:

答案 0 :(得分:0)

您必须确保传入的Intent是唯一的,或者只能调用一次。看起来你一遍又一遍地使用同一个,所以它正在重写。您可能希望将待处理的意图更改为如下所示,并阅读android documentation以设置其他可能的标志

PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent, PendingIntent.FLAG_ONE_SHOT);