gpt4 book ai didi

android - 根据android唯一ID取消alarmmanager pendingintent

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:51 26 4
gpt4 key购买 nike

我正在安排任务并根据日期和时间添加警报管理器,计划任务正在添加到列表中..当我添加任何任务时,它也会添加到 sqlite 数据库中并为警报管理器的待处理 Intent 分配一个唯一的 ID。

现在,如果我想解除警报,那么如果我从列表中删除该行,那么我也想解除该特定警报。我可以从数据库中删除行,但如何解除为该行设置的警报?

我的代码如下:

               Button  AddData=(Button)findViewById(R.id.senddata);
AddData.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);

Date= updatedate.getText().toString();
Time= updateTime.getText().toString();
Discripton= discription.getText().toString();
//---get current date and time---
Calendar calendar = Calendar.getInstance();
//---sets the time for the alarm to trigger---
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.MINUTE, mMinute);
calendar.set(Calendar.SECOND, 0);
Log.i("********####",year+" ,"+month+" , "+day+" , "+mHour+" , "+mMinute+"----"+ calendar.getTimeInMillis());

Intent intent = new Intent(AddEditExpense.this, TimeAlarm.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Bundle b12 = new Bundle();
mStuffresults=Discripton;
b12.putString("serverresponse", mStuffresults);
intent.setAction("" + Math.random());
intent.putExtras(b12);
PendingIntent displayIntent = PendingIntent.getBroadcast(AddEditExpense.this,iUniqueId,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent);

}
}

请参阅此处:已更新

  public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
UniqueId=AddEditExpense.s;
Log.i("UniqueId",UniqueId);
Integer i = Integer.valueOf(UniqueId);
PendingIntent contentIntent = PendingIntent.getBroadcast(TaskReminder.this, i,
new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(contentIntent);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();

return true;
}

最佳答案

您可以检索您为警报创建的 PendingIntent:

PendingIntent displayIntent = PendingIntent.getBroadcast(AddEditExpense.this, iUniqueId, intent, PendingIntent.FLAG_NO_CREATE);

请务必使用您保存在 sqlite 数据库中的唯一 ID,用于此警报。现在您应该取消 AlarmManager 中的 PendingIntent。并取消 PendingIntent本身:

if(displayIntent != null) {
alarmManager.cancel(displayIntent);
displayIntent.cancel();
}

关于android - 根据android唯一ID取消alarmmanager pendingintent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com