【发布时间】:2017-04-04 22:41:47
【问题描述】:
我四处搜寻,发现我的 Android 通知存在问题。
我正在尝试实现一个通知,以便在用户从DatePicker 中选择一个日期时在预定义的时间触发(例如,总是在所选日期的 08:00)。
通知的工作方式与我可以手动触发它一样多,所以我知道它的代码可以工作。
我通过从DatePicker 回调中获取日期来填充我的Calendar,并可以记录它以便我知道我使用的日期是正确的,我将其转换为毫秒以用于设置报警。
如果我选择过去的日期或时间(根据 Android 的默认操作),通知将立即触发,因此它识别我的日期和时间为有效知道它是过去。
但是,问题是,如果我在未来设定时间,它不会触发,我根本不知道为什么。
这是我从 DatePicker 获取年、月和日的 onDateSet() 方法回调,然后设置 HOUR_OF_DAY 和 MINUTE(当前设置为 13:15 以进行测试目的)...
//Add the chosen date to the DB (for the birthday)
//Callback implemented from DatePickerFragment Class
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//Create a new string from the date
String date = String.valueOf(new StringBuilder()
.append(dayOfMonth)
.append("-")
.append(month + 1)
.append("-")
.append(year));
updateDB_Birthday(date);
dbHelper.updateIsOwned(beanieID, 1);
//Convert date set to millis for use with notifying when set
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
long myTime = calendar.getTimeInMillis();
Log.v("z! Calendar", "" + calendar.getTime());
//Create Notification to go off on the beanie's birthday
notificationAlarmManager(myTime);
}
...这是我的通知警报管理器...
private void notificationAlarmManager(long myTime) {
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
notificationReceiver notificationReceiver = new notificationReceiver();
IntentFilter filter = new IntentFilter("ALARM_ACTION");
registerReceiver(notificationReceiver, filter);
Intent intent = new Intent("ALARM_ACTION");
intent.putExtra("param", "My scheduled action");
PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, myTime, operation);
}
...这是我的 BroadcastReceiver 类...
public class notificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
android.support.v4.app.NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(beanieImage)
.setContentTitle("Beanie Notification")
.setContentText("It's " + beanieName + "'s Birthday today!")
.setDefaults(Notification.DEFAULT_ALL);
//Add an intent
Intent notificationIntent = new Intent(context, ItemDetail.class);
notificationIntent.putExtra("EXTRAS_ID", beanieID);
notificationIntent.putExtra("EXTRAS_NAME", beanieName);
notificationIntent.putExtra("EXTRAS_IMAGE", beanieImage);
notificationIntent.putExtra("EXTRAS_BIRTHDAY", beanieBirthday);
notificationIntent.putExtra("EXTRAS_IS_OWNED", isOwned);
//Add pending intent
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Launch activity on click of notification
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(001, builder.build());
}
}
我已经被困在这个问题上几天了,这是完成这个应用程序所需的最后一件事,所以任何帮助都将非常感谢。
提前致谢,
附言 我昨天就这个问题问了一个类似的问题here,但显然我的问题“没有显示出足够的研究努力”,即使是我的研究让我走到了这一步。我已经做了很多“功课”,所以请不要以为我会轻率地参加这个论坛,这几乎是最后的请求。
【问题讨论】:
-
AlarmManager请求在重新启动时被删除。这可能是这里的原因吗? -
对于我的测试,我将它设置为未来几分钟,并且只是坐着看着指定的时间来来去去,所以我不这么认为。另外,我读到这就是 BroadcastReciever 的用途。
-
@MikeM。我的错,下次我会记住这一点,谢谢。
-
设置闹钟后是否要关闭应用?您正在动态注册接收器,因此如果
Context它打开了,您的警报将不会做任何事情。您应该改为在清单中注册 Receiver 类,并使用显式Intent进行广播。即,指定接收器类的Intent。此外,AlarmManager#set()自 KitKat 以来并不准确,如果您的targetSdkVersion>= 19,请在等待闹钟时记住这一点。 -
@MikeM。不,正如我所说,我正在设置闹钟并在盯着屏幕等待时间结束的同时睁大眼睛。那么像我这样设置接收器是行不通的吗?有什么改变吗?请告诉我清单需要什么,我看到有些人在不同的地方添加了各种各样的东西,但没有一致的东西。
标签: android date datepicker notifications alarmmanager