【问题标题】:Unable to cancel PendingIntent from AlarmManager无法从 AlarmManager 取消 PendingIntent
【发布时间】:2013-10-22 06:19:09
【问题描述】:

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

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

这是来自 2 个类的代码,一个是我创建警报的地方,另一个是我取消警报的地方,不知道我错过了什么!!!

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 我正在从数据库中删除警报并尝试取消警报管理器。在这里,我使用了相同的 AlarmId,它用于创建 PendingIntent

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.
}

我在 Google 上搜索了很多和 stackoverflow,但在任何地方我都看到这个解决方案运行良好,但它不适合我。

我认为我在这里注意到通知的 Onlick,我正在使用 CreateActivity 类来显示详细信息,并且在更新时,它会更新通知的警报。我想我在与IntentPendingIntent 相关的上下文中搞砸了。

请帮帮我。

【问题讨论】:

    标签: java android alarmmanager


    【解决方案1】:

    添加这个:

    myIntent.putExtra("alarmId", alarmId);
    

    到您的 deleteReminder() 方法(或将其从 setAlarm 中删除)。

    为了删除PendingIntent,它必须完全匹配。

    【讨论】:

    • 我有一个类似的用例,在原始 Intent 上有一个 setAction()。在我在 delete() 方法中将完全相同的 setAction() 添加到 pendingIntent 之前,pendingIntent 才被取消。
    【解决方案2】:

    参考这个。 Delete alarm from AlarmManager using cancel() - Android

    根据那里提供的回答,您的删除方法中的pendingIntent 应声明为

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多