从AlarmManager取消警报

时间:2016-05-11 11:43:32

标签: android alarmmanager

我正在处理应用程序,它每隔凌晨2点从数据库获取闹钟时间,然后在活动显示的指定时间,我的应用程序工作正常,但是当我手动更改设备的数据/时间时,之前的警报被触发,我应该防止这种情况发生。

这是我的private CharSequence renderNumericInput(BeanPropertyAccessor propertyAccessor,Map model, Map attrs) { if (!attrs.type && model.constraints?.inList) { attrs.from = model.constraints.inList if (!model.required) attrs.noSelection = ["": ""] return g.select(attrs) } else if (model.constraints?.range) { attrs.type = attrs.type ?: "range" attrs.min = model.constraints.range.from attrs.max = model.constraints.range.to } else { attrs.type = attrs.type ?: getDefaultNumberType(model ) if (model.constraints?.scale != null) attrs.step = "0.${'0' * (model.constraints.scale - 1)}1" if (model.constraints?.min != null) attrs.min = model.constraints.min if (model.constraints?.max != null) attrs.max = model.constraints.max } if(propertyAccessor != null && attrs.value) { attrs.value = g.fieldValue(bean: propertyAccessor.rootBean, field: propertyAccessor.propertyName) // This call causes the Error // Debugging Values: propertyAccessor.rootBean: RootBean, // propertyAccessor.propertyName: nestedInteger } return g.field(attrs) }

PendingIntent

每个 Intent i = new Intent(mContext, AlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(G.context, _id, i, PendingIntent.FLAG_UPDATE_CURRENT); 都是唯一的,当我在警报取消方法触发之前警报响起之前我为日期更改事件设置了接收器时!

有些人可以帮助解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

试试这段代码:

Intent intent = new Intent(this, Mote.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent);

答案 1 :(得分:0)

取消闹钟:

 Intent intent = new Intent(this, Mote.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 alarmManager.cancel(pendingIntent);

修改

您可以根据自己的要求寻找答案并进行修改:

How to stop Android AlarmManager when there is a manual system time change?

AlarmManager: how to schedule a daily alarm and deal with time changes

答案 2 :(得分:0)

 Intent intent1 = new Intent(G.context, DailyAlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context,
     G.dateTimesetAlarm.getDayOfYear(), intent1, 0);
 mAlarmManager.cancel(pendingIntent);
 pendingIntent.cancel();