Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从AlarmManager取消挂起事件_Java_Android_Alarmmanager - Fatal编程技术网

Java 无法从AlarmManager取消挂起事件

Java 无法从AlarmManager取消挂起事件,java,android,alarmmanager,Java,Android,Alarmmanager,我是Android新手,我正在创建一个日期提醒应用程序,用户将在其中输入日期、时间和消息,以便在输入的日期和时间收到通知。到目前为止,我已经实现了创建报警、显示通知、单击通知、打开应用程序以及该通知的详细信息,并在需要时进行更新,列出所有报警(我使用SqlLite存储数据) 在“我的报警列表”页面上,单击项目,我将显示带有删除选项的报警对话框,当我单击删除时,我将从数据库中删除报警详细信息,但无法取消报警 这是两个类的代码,一个是我创建报警,另一个是我取消报警,不确定我错过了什么 Class C

我是Android新手,我正在创建一个日期提醒应用程序,用户将在其中输入日期、时间和消息,以便在输入的日期和时间收到通知。到目前为止,我已经实现了创建报警、显示通知、单击通知、打开应用程序以及该通知的详细信息,并在需要时进行更新,列出所有报警(我使用SqlLite存储数据)

在“我的报警列表”页面上,单击项目,我将显示带有删除选项的报警对话框,当我单击删除时,我将从数据库中删除报警详细信息,但无法取消报警

这是两个类的代码,一个是我创建报警,另一个是我取消报警,不确定我错过了什么

Class CreateActivity extends Activity
methode setAlarm{
.....
        Intent myIntent = new Intent(this.getApplicationContext(), AlarmReciever.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        db = new DatabaseHandler(this);
        if(!isNotified) //create new alarm
        {
            Random random = new Random();
            alarmId  = random.nextInt(10000);
            pojo = new DateBookPojo(alarmId, inDate, inTime, inMsg);
            db.addReminder(pojo);
            System.out.println("Adding Reminder.");
        }else{ // Its from notification
            pojo = new DateBookPojo(alarmId, inDate, inTime, inMsg);
            db.updateReminder(pojo);
            System.out.println("Updating Reminder.");
        }

        myIntent.putExtra("alarmId", alarmId);

        pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
....
}

Here AlarmId is unique passed to PendingIntent
这里是另一个列表活动,单击AlertDialog,我将从数据库中删除报警并尝试取消报警管理器。这里我使用了与创建PendingEvent相同的AlarmId

Class MyListActivity extends ListActivity
....
private void deleteReminder(int alarmId) {
    System.out.println("alarmId= "+ alarmId);
    DatabaseHandler db = new DatabaseHandler(this);
    DateBookPojo pojo = new DateBookPojo();
    pojo.setReminderId(alarmId);
    db.deleteReminder(pojo); // delete alarm details from database
    System.out.println("Deleted Entry. = > " + alarmId);

    Intent myIntent = new Intent(this.getApplicationContext(), AlarmReciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    System.out.println("Cancelled Alarm. = > " + alarmId);

    showList(); // Again show the new list.
}
我在谷歌上搜索了很多次,但无论在哪里,我都看到这个解决方案工作得很好,但它对我不起作用

我在这里注意到,单击通知,我使用
CreateActivity
类显示详细信息,当更新时,它会更新通知的报警。我想我把与
Intent
pendingent
相关的上下文搞乱了

请帮助我。

添加以下内容:

myIntent.putExtra("alarmId", alarmId);
添加到您的
deletemrements()
方法(或将其从
setAlarm
中删除)

要删除挂起内容,它必须精确匹配。

请参阅此。

根据那里提供的答案,删除方法中的
pendingent
应声明为

PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

我有一个类似的用例,在原始意图上有一个setAction()。直到我在delete()方法中将完全相同的setAction()添加到PendingEvent中,PendingEvent才被取消。