如何取消特定的闹钟?

发布于 2024-12-11 06:59:17 字数 3168 浏览 2 评论 0原文

我在取消特定警报时遇到问题。我正在使用警报管理器 SET 方法设置多个警报。但我无法取消特定的警报。如果我取消,则取消之前设置的所有闹钟。我使用单个警报管理器 ID 来设置多个警报。

保存方法

private void saveState() 
{
    //  String date = mDateText.getText().toString();
    String title = mTitleText.getText().toString(); 
    String body = mBodyText.getText().toString();
    String sdate = sDateText.getText().toString();
    String stime = sTimeText.getText().toString();   
    String rdate = rDateText.getText().toString();   
    String rtime = rTimeText.getText().toString();   

    String appRemDate = ((Button)findViewById(R.id.enterdaterem)).getText().toString();
    String appRemTime = ((Button)findViewById(R.id.entertimerem)).getText().toString();


    Calendar cal = Calendar.getInstance(); 

    //setting date & month
    if(appRemDate.substring(0,2).endsWith("-" ) && appRemDate.substring(2,4).endsWith("-" ))
    { 

        cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(4,8)));
        cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(2,3)) -1);
        cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,1))); 
    }
    else if( !appRemDate.substring(0,2).contains("-" ) && !appRemDate.substring(3,5).contains("-" ) )
    {  

        cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(6,10)));
        cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(3,5)) -1);
        cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,2))); 
    }
    else if( appRemDate.substring(0,2).endsWith("-" ) && !appRemDate.substring(2,4).endsWith("-" ))
    {  
        cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(5,9)));
        cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(2,4)) -1);
        cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,1))); 
    }
    else  
    {  
        cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(5,9)));
        cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(3,4)) -1);
        cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,2))); 
    } 
    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(appRemTime.substring(0, 2))); 

    cal.set(Calendar.MINUTE, Integer.parseInt(appRemTime.substring(3, 5)));

    cal.set(Calendar.SECOND, 0); 

    if (mRowId == null) 
    {
        long id = mDbHelper.createNote(  title, body, sdate, stime, rdate, rtime);
        invokeAlaram( cal.getTimeInMillis(), id );
        if (id > 0) 
        {
            mRowId = id;
        }
    } 
    else 
    { 
        long id = mDbHelper.updateNote(mRowId,  title, body, sdate, stime, rdate, rtime);
        invokeAlaram( cal.getTimeInMillis(),   id );
    }
}

调用警报方法

private void invokeAlaram(long invokeTime, long id )
{

    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 

    Intent i =  new Intent(this, MyAlarmServiceForAppointments.class);

    i.putExtra("rowId", String.valueOf(id));
    am.set(AlarmManager.RTC_WAKEUP, invokeTime,   PendingIntent.getService(this , (int) System.currentTimeMillis() ,i , 0));

} 

任何人都可以帮助我如何取消特定警报。

I am facing problem with canceling the particular alarm. I am setting multiple alarm using alarm manager SET method. But i am not able to cancel the particular alarm. If i cancel, it is canceling all the alarm previously set. I am using single ALARM MANAGER ID to set the multiple alarms.

SAVE METHOD

private void saveState() 
{
    //  String date = mDateText.getText().toString();
    String title = mTitleText.getText().toString(); 
    String body = mBodyText.getText().toString();
    String sdate = sDateText.getText().toString();
    String stime = sTimeText.getText().toString();   
    String rdate = rDateText.getText().toString();   
    String rtime = rTimeText.getText().toString();   

    String appRemDate = ((Button)findViewById(R.id.enterdaterem)).getText().toString();
    String appRemTime = ((Button)findViewById(R.id.entertimerem)).getText().toString();


    Calendar cal = Calendar.getInstance(); 

    //setting date & month
    if(appRemDate.substring(0,2).endsWith("-" ) && appRemDate.substring(2,4).endsWith("-" ))
    { 

        cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(4,8)));
        cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(2,3)) -1);
        cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,1))); 
    }
    else if( !appRemDate.substring(0,2).contains("-" ) && !appRemDate.substring(3,5).contains("-" ) )
    {  

        cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(6,10)));
        cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(3,5)) -1);
        cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,2))); 
    }
    else if( appRemDate.substring(0,2).endsWith("-" ) && !appRemDate.substring(2,4).endsWith("-" ))
    {  
        cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(5,9)));
        cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(2,4)) -1);
        cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,1))); 
    }
    else  
    {  
        cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(5,9)));
        cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(3,4)) -1);
        cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,2))); 
    } 
    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(appRemTime.substring(0, 2))); 

    cal.set(Calendar.MINUTE, Integer.parseInt(appRemTime.substring(3, 5)));

    cal.set(Calendar.SECOND, 0); 

    if (mRowId == null) 
    {
        long id = mDbHelper.createNote(  title, body, sdate, stime, rdate, rtime);
        invokeAlaram( cal.getTimeInMillis(), id );
        if (id > 0) 
        {
            mRowId = id;
        }
    } 
    else 
    { 
        long id = mDbHelper.updateNote(mRowId,  title, body, sdate, stime, rdate, rtime);
        invokeAlaram( cal.getTimeInMillis(),   id );
    }
}

INVOKE ALARM METHOD

private void invokeAlaram(long invokeTime, long id )
{

    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 

    Intent i =  new Intent(this, MyAlarmServiceForAppointments.class);

    i.putExtra("rowId", String.valueOf(id));
    am.set(AlarmManager.RTC_WAKEUP, invokeTime,   PendingIntent.getService(this , (int) System.currentTimeMillis() ,i , 0));

} 

Can anyone help me how to cancel the particular alarm.

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

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

发布评论

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

评论(1

夜清冷一曲。 2024-12-18 06:59:17

当您将请求代码的系统毫秒时间传递到 getService() 时,

am.set(AlarmManager.RTC_WAKEUP, invokeTime,   PendingIntent.getService(this , (int) System.currentTimeMillis() ,i , 0));

而不是使用系统时间,您必须传递您的唯一编号,该编号被认为是请求与特定意图不同。

现在您必须在此基础上保留所有请求代码,您可以取消警报

注意:作为您传递到 PendingIntent 的 Intent 对象,该对象必须与您在设定时间定义的取消警报的定义相同

am.set(AlarmManager.RTC_WAKEUP, invokeTime,   PendingIntent.getService(this , 1 ,i , 0));

am.set(AlarmManager.RTC_WAKEUP, invokeTime,   PendingIntent.getService(this , 2 ,i , 0));

,等等

现在用于取消

am.cancel(PendingIntent.getService(this , 2 ,i , 0));

as you pass the system millisecond time for the request code into the getService() like here

am.set(AlarmManager.RTC_WAKEUP, invokeTime,   PendingIntent.getService(this , (int) System.currentTimeMillis() ,i , 0));

instead of using system time you have to pass your unique number which consider as request was different for specific intent.

Now you have to keep this all request code based on this you can cancel the alarm

Note: As the Intent object you pass into the PendingIntent that must be same define for cancelling the alarm as you defined at set time

am.set(AlarmManager.RTC_WAKEUP, invokeTime,   PendingIntent.getService(this , 1 ,i , 0));

am.set(AlarmManager.RTC_WAKEUP, invokeTime,   PendingIntent.getService(this , 2 ,i , 0));

and so on

now for cancel

am.cancel(PendingIntent.getService(this , 2 ,i , 0));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文