Android,如何取消闹钟? alarmManager.cancel 不工作
Posted
技术标签:
【中文标题】Android,如何取消闹钟? alarmManager.cancel 不工作【英文标题】:Android, How to cancel an alarm? alarmManager.cancel not working 【发布时间】:2015-02-27 02:43:10 【问题描述】:我有一个应用程序,您可以在其中创建 5 个将要创建的警报显示在 ListView 中,并假设当 listView 的一个元素播放警报但应该从 listview 中删除该项目但警报被删除以到达预定的时间总是响起。 我已经查看了很多关于如何删除警报的教程和指南,但没有一个对我有帮助。 要删除我所做的是将每个警报的 ID 保存在 sqlite 数据库中,当我给出警报 eliminiar 时,我所做的就是将要消除的警报的 ID 提供给 PendingIntent.getBroadcast 以删除您的警报想要但它不会发生。 有人可以检查我的代码并告诉我是否有任何错误出现,这样就不会消除警报。 谢谢你
我用来创建警报的方法
public void agregarAlarma(Calendar targetCal)
final int _id = ((int)System.currentTimeMillis()/1000);
Random generator = new Random();
DbHelper helper = new DbHelper(this);
SQLiteDatabase bd = helper.getWritableDatabase();
int id_nuevo = (_id + generator.nextInt());
if(alarma.getSelectedItem().toString().equals("Desayuno"))
comida = alarma.getSelectedItem().toString() + " " + targetCal.getTime().toString();
descripcion = "del desayuno";
else if(alarma.getSelectedItem().toString().equals("Primera Merienda"))
comida = alarma.getSelectedItem().toString() + " " + targetCal.getTime().toString();
descripcion = "de la primera merienda";
else if(alarma.getSelectedItem().toString().equals("Almuerzo"))
comida = alarma.getSelectedItem().toString() + " " + targetCal.getTime().toString();
descripcion = "del almuerzo";
else if(alarma.getSelectedItem().toString().equals("Segunda Merienda"))
comida = alarma.getSelectedItem().toString() + " " + targetCal.getTime().toString();
descripcion = "de la segunda merienda";
else if(alarma.getSelectedItem().toString().equals("Cena"))
comida = alarma.getSelectedItem().toString() + " " + targetCal.getTime().toString();
descripcion = "de la cena";
Intent intent;
PendingIntent pendingIntent;
AlarmManager alarmManager;
if(tablaVacia()>= 5)
Toast.makeText(this,"Esta lista solo permite 5 alarmas, elimine alguna para crear otra nueva",Toast.LENGTH_SHORT).show();
else
intent = new Intent(Recordatorios2.this, AlarmReceiver.class);
intent.putExtra("Comida",comida);
pendingIntent = PendingIntent.getBroadcast(Recordatorios2.this, id_nuevo ,intent, PendingIntent.FLAG_ONE_SHOT);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
descripcionTotal = "Alarma " + descripcion + " creada "+ targetCal.getTime().toString();
Toast.makeText(this,descripcionTotal,Toast.LENGTH_SHORT).show();
ContentValues valores = new ContentValues();
valores.put("Descripcion",comida);
valores.put("Identificador",id_nuevo);
bd.insert("Alarmas",null,valores);
llenarListaAlarmas1();
这填满了listView
public void llenarListaAlarmas1()
listaAlarmas = (ListView)findViewById(R.id.listAlarma);
lista.clear();
DbHelper helper = new DbHelper(this);
SQLiteDatabase bd = helper.getWritableDatabase();
Cursor c = bd.rawQuery(CONSULTA_ALARMAS,null);
if(c.moveToFirst())
do
alarmaDatos = new AlarmaDatos(c.getString(0),null);
lista.add(alarmaDatos);
while (c.moveToNext());
final ArrayAdapter<AlarmaDatos> adapter1 = new ArrayAdapter<AlarmaDatos>(getApplicationContext(),android.R.layout.simple_list_item_1,lista);
adapter1.notifyDataSetChanged();
listaAlarmas.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listaAlarmas.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView adapterView, View view, int position, long id)
switch (position)
case 0:
seleccion = adapter1.getItem(position).toString();
eliminarAlarma();
adapter1.notifyDataSetChanged();
lista.remove(position);
break;
case 1:
seleccion = adapter1.getItem(position).toString();
eliminarAlarma();
adapter1.notifyDataSetChanged();
lista.remove(position);
break;
case 2:
seleccion = adapter1.getItem(position).toString();
eliminarAlarma();
adapter1.notifyDataSetChanged();
lista.remove(position);
break;
case 3:
seleccion = adapter1.getItem(position).toString();
eliminarAlarma();
adapter1.notifyDataSetChanged();
lista.remove(position);
break;
case 4:
seleccion = adapter1.getItem(position).toString();
eliminarAlarma();
adapter1.notifyDataSetChanged();
lista.remove(position);
break;
);
这是用来消除警报的一种
public void eliminarAlarma()
DbHelper helper = new DbHelper(getApplicationContext());
SQLiteDatabase bd = helper.getWritableDatabase();
int identificador =0;
String CONSULTA_ID = "SELECT Identificador FROM Alarmas WHERE Alarmas.Descripcion = '" + seleccion + "';" ;
Cursor cursor = bd.rawQuery(CONSULTA_ID,null);
if(cursor.moveToFirst())
do
alarmaDatos = new AlarmaDatos(cursor.getString(0),null);
identificador = Integer.parseInt(alarmaDatos.toString());
while (cursor.moveToNext());
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), identificador ,intent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(getApplicationContext().ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
borrar(identificador);
Toast.makeText(getApplicationContext(),"Alarma eliminada",Toast.LENGTH_SHORT).show();
有人能帮帮我吗,因为事实并不是因为如果一切看起来都很好,它就消除了警报。
TKS
【问题讨论】:
【参考方案1】:不用打pendingIntent.cancel();
,把那条线去掉。
此外,PendingIntent 的构造应该与您安排 AlarmManager 时完全相同。
根据文档,两个 Intent 被认为是相等的,如果:
它们的动作、数据、类型、类和类别是相同的。这不会比较意图中包含的任何额外数据。
所以应该是这样的:
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), identificador , intent, PendingIntent.FLAG_NO_CREATE);
AlarmManager alarmManager = (AlarmManager)getSystemService(getApplicationContext().ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
【讨论】:
非常感谢您为我节省了很多时间。 intent 中的 extras 不用于比较,因此不需要包含它们。见developer.android.com/reference/android/app/… @kp91 看起来你是对的。谢谢!有机会我会更新答案...【参考方案2】:创建
preference_saved_value = DatabaseConstants.NOTIFICATION_ID
sender = PendingIntent.getBroadcast(this,preference_saved_value, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
取消
sender = PendingIntent.getBroadcast(this,
preference_saved_value, intent,PendingIntent.FLAG_UPDATE_CURRENT);
sender.cancel()
【讨论】:
丹尼尔,非常感谢你,你不知道它让我头疼。我有很多小时来寻找为什么不删除,这就像添加一行代码一样简单。(intent.putExtra("Comida",comida);) 你碰巧知道我可以做些什么来制作警报记录是即使手机重置也可以存储?是做一些研究,再次非常感谢。 dhams,如果单元格保持存储状态,这将用于存储警报重置?也感谢您通过邮件停止并给我您的帮助 @alexandermatafonseca 您需要在手机重启时注册所有警报。设备重启后所有警报取消 嗯,我想我应该能够在警报程序出现时以某种方式存储和阻止手机。我会调查一下以上是关于Android,如何取消闹钟? alarmManager.cancel 不工作的主要内容,如果未能解决你的问题,请参考以下文章