【发布时间】:2012-04-22 00:08:57
【问题描述】:
我正在尝试获取在特定时间触发的通知。我突然想到我不必使用警报管理器,因为我有设置时间的“alCal”变量。是否可以将 alCal 转换为 Long 并使用它而不是 System.currentTimeMillis(); 作为 fireTime 变量?如果是这样,我将如何将 alCal 转换为 Long?这是我当前的代码:
//---use the AlarmManager to trigger an alarm---
AlarmManager aMan = (AlarmManager) getSystemService(ALARM_SERVICE);
//---get current date and time---
Calendar alCal = Calendar.getInstance();
//---sets the time for the alarm to trigger---
alCal.set(Calendar.HOUR_OF_DAY, 23);
alCal.set(Calendar.MINUTE, 41);
alCal.set(Calendar.SECOND, 00);
Intent noteIntent = new Intent(this, Splash.class);
PendingIntent pendI = PendingIntent.getActivity(this, 0, noteIntent, 0);
long fireTime = System.currentTimeMillis();
String noteBody = "notification body";
String noteTitle = "notification title";
Notification note = new Notification(R.drawable.noteicon, noteTitle, fireTime);
note.setLatestEventInfo(this, noteTitle, noteBody, pendI);
note.defaults = Notification.DEFAULT_SOUND;
noteman.notify(uniqueID, note);
//---sets the alarm to trigger---
aMan.set(AlarmManager.RTC_WAKEUP, alCal.getTimeInMillis(), pendI);
finish();
好的,看起来我无法将 alCal 转换为 long,所以我想我必须使用 AlarmManager。那么给定上面的代码,我该如何修改它以便AlarmManger在指定的时间触发通知?我是 Android 和 Java 的新手,所以我并没有真正看到这个问题。我认为必须在新的通知调用中调用 alCal,但我不知道该怎么做。 Eclipse 说我不能把 fireTime 换成 alCal,所以我不知道下一步该尝试什么。
【问题讨论】:
-
您正在使用
alCal.getTimeInMillis将alCal“转换”为long...您的问题是什么? -
您在 2 小时前发布了类似的问题,我回答了。您是否尝试过该解决方案?我现在没有看到这个问题。你删了吗?
-
是的,我确实删除了这个问题,因为我认为这是一个更好的问题。但也许不是。我确实尝试了您的解决方案,并在其中放置了一个日期变量使应用程序崩溃。
标签: android android-notifications notificationmanager