Android,如何取消闹钟? alarmManager.cancel无效

时间:2015-02-27 02:43:10

标签: android android-listview alarmmanager alarm android-alarms

我有一个应用程序,您可以在其中创建5个警报,其中创建的内容显示在ListView中,并假设当listView的元素播放了警报但应该从列表视图中删除该项目但是警报被删除以达到预定的时间总是响起。 我已经查看了很多关于如何删除警报的教程和指南,但没有人帮助我。 要删除我所做的是将每个警报的ID保存在sqlite数据库中,当我发出警报时,我做的是我带来要消除的警报的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

2 个答案:

答案 0 :(得分:5)

无需致电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);

答案 1 :(得分:1)

  

创建

preference_saved_value =  DatabaseConstants.NOTIFICATION_ID
sender = PendingIntent.getBroadcast(this,preference_saved_value, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
  

CANCEL

sender = PendingIntent.getBroadcast(this, 
preference_saved_value, intent,PendingIntent.FLAG_UPDATE_CURRENT);  
sender.cancel()