微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以根据次数停止警报管理器

如何解决是否可以根据次数停止警报管理器

请帮助。。谢谢。

应用程序运行正常,但是我需要在无时间后停止闹钟(以整数表示)-我添加了此功能,以便在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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?