根据时间选择器和微调器值设置通知

Setting notifications based on timepicker and Spinner value

我有两个时间选择器和微调器,值为 1 到 10,我想在从两个时间选择器选择的时间之间从微调器中选择本地通知的数量,例如从下午 2 点到下午 6 点我想显示六个通知它应该每天在选定的时间范围和选定的值之间通知,我为此使用了警报管理器,但它只显示一个通知也使用了设置重复。

我也试图首先找到两个选择器的时间差(以毫秒为单位)并应用了一些逻辑,但似乎没有什么是好的。请给我一些逻辑或示例代码将不胜感激。

一些代码片段是

Intent myIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    myIntent, 0);
long next = 0;
for (int i = 0; i < spinnervalue ); i++) {
    Log.e("checkkkk ", "" + "checkkkk");
    alarmManager.set(AlarmManager.RTC_WAKEUP,  next, pendingIntent);
    next += diff_millis;
}

http://developer.android.com/reference/android/app/AlarmManager.html#set%28int,%20long,%20android.app.PendingIntent%29

If the stated trigger time is in the past, the alarm will be triggered immediately. If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.

所以循环设置这不是你想要的。

您的毫秒值需要是时间。 0 表示纪元(1970 年 1 月 1 日)。您可能想要的是 "SystemClock.elapsedRealtime() + someValue" 传入该参数。

此代码段应该可以工作,也许需要进行一些更改。 在这里,我得到了以毫秒为单位的 2 个时间选择器和以毫秒为单位的当前时间 += 之间的差异。为每个挂起的意图提供不同的 ID,并告诉它更新现有的 ID(如果您知道 ID(计数器),则允许您重置它们)。 您必须将差异除以所选的微调器以获得均匀间隔的毫秒差异。

//Assume timepicker2 > timepicker1
int hours1 = timePicker1.getCurrentHour();
int minutes1 = timePicker1.getCurrentMinute();
int hours2 = timePicker2.getCurrentHour();
int minutes2 = timePicker2.getCurrentMinute();

int hourDiff = hours2-hours1;
int minutesDiff = minutes2-minutes1
int millisecondsDiff = hourDiff*60*60*1000;
millisecondsDiff += minutesDiff*60*1000;
Double period_diff = millisecondsDiff/spinnerValue;

//This is for the intent difference
int counter = 0;
long next = System.currentTimeMillis() + period_diff;

for (int i = 0; i < spinnervalue ); i++) {
    Intent myIntent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, counter++, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Log.e("checkkkk ", "" + "checkkkk");
    alarmManager.set(AlarmManager.RTC_WAKEUP,  next, pendingIntent);
    next += period_diff;
}

不要将代码段作为最终用途,因为它是快速记下的,但它应该让您大致了解应该查看的位置。

另一个想法是使用 i 作为计数器,以便您知道 ID 的范围。