如何解决是否可以根据次数停止警报管理器
请帮助。。谢谢。
应用程序运行正常,但是我需要在无时间后停止闹钟(以整数表示)-我添加了此功能,以便在9天后停止闹钟。Getting Input public void setRepeatAlarm(上下文上下文,日历日历,整数ID,长RepeatTime){ mAlarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
// Put Reminder ID in Intent Extra
Intent intent = new Intent(context,AlarmReceiver.class);
intent.putExtra(ReminderEditactivity.EXTRA_REMINDER_ID,Integer.toString(ID));
mPendingIntent = PendingIntent.getbroadcast(context,ID,intent,PendingIntent.FLAG_CANCEL_CURRENT);
// Calculate notification timein
Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
long diffTime = calendar.getTimeInMillis() - currentTime;
// Start alarm using initial notification time and repeat interval time
mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime() + diffTime,RepeatTime,mPendingIntent);
// Restart alarm if device is rebooted
ComponentName receiver = new ComponentName(context,BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
}
如何根据次数来停止重复警报。(例如:开始日期为12-11-2020,结束日期为15-12-2020-im输入的次数为无次数(3次)不为结束日期。) 怎么做。请帮忙
谢谢..
解决方法
您可以在SharedPreferences
之类的位置存储警报触发的次数和警报需要触发的次数,并在每次执行之前检查该次数是否更高。
另一种解决方案是计算x
天中的日期,然后将该日期保存在SharedPreferences
中,并在每次执行之前检查今天是否是我们保存的日期,如果是,请取消该警报。
class MySharedPreferences(context: Context) {
private val preferences: SharedPreferences = context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE)
private val preferencesEditor: SharedPreferences.Editor = preferences.edit()
fun setDeadlineOfAlarm(days: Int) {
preferencesEditor.putInt(PREF_KEY_DEADLINE,days)
preferencesEditor.apply()
}
fun setCurrentDaysTriggered(days: Int) {
preferencesEditor.putInt(PREF_KEY_CURRENT_DAYS,days)
preferencesEditor.apply()
}
fun getDeadlineOfAlarm(): Int = preferences.getInt(PREF_KEY_DEADLINE,0)
fun getCurrentDaysTriggered(): Int = preferences.getInt(PREF_KEY_CURRENT_DAYS,0)
companion object {
private const val PREF_FILE_NAME = "AlarmPreferences"
private const val PREF_KEY_DEADLINE = "Alarm_Deadline"
private const val PREF_KEY_CURRENT_DAYS = "Alarm_Current_Days"
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。