【问题标题】:Cancelling alarm using AlarmManager使用 AlarmManager 取消警报
【发布时间】:2017-06-15 12:37:58
【问题描述】:

我正在开发一个提醒应用程序,我在其中存储所有提醒 使用sqlite在数据库中的相关数据,一切正常。

问题:

当我从数据库中删除行时,我也在尝试将 警报关闭,但它不起作用,我正在使用 recyclerview。

请帮我找出我的代码有什么问题。

这是我的代码:

DataAdapter.java

dataAdapterViewHolder.deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int deleteRowId = context.getContentResolver().delete(RoutineContract.RoutinesEntry.CONTENT_URI,
                    rowSelection, rowSelectionArgs);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, AlarmReceiver.class);
            PendingIntent alertIntent = PendingIntent.getBroadcast(context, rowId, intent, PendingIntent.FLAG_NO_CREATE);
            if(alertIntent != null) {
                alarmManager.cancel(alertIntent);
                alertIntent.cancel();
                Log.e("row id", ""+rowId);
            }
        }
    });

AddReminderActivity.java

            Uri insertUri = getContentResolver().insert(RoutineContract.RoutinesEntry.CONTENT_URI, contentValues);
            int rowId = Integer.parseInt(insertUri.getLastPathSegment());

            Intent intent = new Intent(this, AlarmReceiver.class);
            intent.setData(insertUri);
            intent.putExtra("name", name);
            intent.putExtra("repeatType", repeatType);
            PendingIntent alertIntent = PendingIntent.getBroadcast(this, rowId, intent, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

            alarmManager.set(AlarmManager.RTC_WAKEUP, dateTime.getTimeInMillis(),
            pendingIntent);

P.S.:我已经在 stackoverflow 上搜索并找到了问题的解决方案,但现在没有一个对我有用。

【问题讨论】:

    标签: android alarmmanager android-notifications android-alarms


    【解决方案1】:

    当您使用此代码 PendingIntent alertIntent = PendingIntent.getBroadcast(context, rowId, intent, PendingIntent.FLAG_NO_CREATE); 检查警报意图是否已经存在时,您传递标志PendingIntent.FLAG_NO_CREATE,如果找到,则返回null,您可以阅读更多here。 尝试改为传递PendingIntent.FLAG_UPDATE_CURRENT

    编辑:我尝试使用与您类似的代码安排和取消警报,它似乎可以工作,主要区别在于传递给 getBroadcast 函数的 Intent 和我用来获取pendingIntent,在这两种情况下都是FLAG_UPDATE_CURRENT。我使用了下面的代码

    public void scheduleAlarm() {
            Intent notificationIntent = new Intent(mContext, ScheduledActionReceiver.class);
            notificationIntent.setAction(ACTION_NOTIFICATION);
    
            long now = System.currentTimeMillis() + 5 * 1000;
    
            AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, Constants.ALARM_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmManager.set(AlarmManager.RTC_WAKEUP, now, pendingIntent);
    }
    public void removeAlarm() {
            Intent notificationIntent = new Intent(mContext, ScheduledActionReceiver.class);
            notificationIntent.setAction(ACTION_NOTIFICATION);
    
            AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, Constants.ALARM_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmManager.cancel(pendingIntent);
    }
    

    编辑 1:我想我终于弄明白了,经过更多的测试,我注意到如果输入这行代码 notificationIntent.setData(Uri.parse("uri to parse"));

    当我安排闹钟时,如果我在取消它之前检索它时也没有放置它,那么取消操作将不起作用(另一方面,设置附加功能似乎没有任何区别)

    【讨论】:

    • 我也尝试过使用 FLAG_UPDATE_CURRENT 和 FLAG_ONE_SHOT,但这对我不起作用
    • 请查看我编辑的答案并尝试在取消警报时添加 setData(yourUri)
    • 谢谢,它在文档中提到取消pendingIntent应该与创建时使用的匹配,我忘记了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    相关资源
    最近更新 更多