如何使每次对 AlarmManager 的调用都独一无二?

发布于 2024-10-19 10:46:16 字数 1600 浏览 6 评论 0原文

我在安排闹钟时遇到问题。我想让我的闹钟的每个呼叫都是唯一的,这样它就不会与我已经设置的上一个闹钟重叠。 这是我的代码:

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 时,它只设置最新的一个。如何使这里的每一套都独一无二?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-10-26 10:46:16

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文