2011-02-26 15 views
1

我在安排闹钟时遇到问题。我想让每个警报的唯一呼叫都是独一无二的,这样它就不会重叠我之前设置的警报。 这是我的代码:如何让我的每个电话都与我的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时,它只设置最新的一个。我如何使每一组都具有独特性?

回答

0

您必须确保您传入的Intent是唯一的或者只能被调用一次。它看起来像你一遍又一遍地使用同一个,所以它已经越过了书面。你可能想改变你的悬而未决的意图看起来像什么如下,读android documentation其他可能的标志设置

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

我已经试过了,但它不会工作:( – madel 2011-02-27 03:48:15

+0

我已经得到了答案。您必须在你的alarmManager中为pendingIntent插入这段代码。 – madel 2011-03-02 16:17:49

相关问题