我想根据日期安排通知(日期为PostScheduledDate
),通知将在PostScheduledDate
之前,before
的时间为1分钟我的代码。
我试过了:
//here am converting the postScheduledDate to milliseconds
Long postScheduledLong = postScheduledDate.getTime();
// and here converting system time to millisecond
Long systemsCurrentTime = System.currentTimeMillis();
// and here am getting the difference between system time and postscheduled time
Long differneceToAdd = systemsCurrentTime - postScheduledLong;
// here am converting that difference to int so that i can put the int value to my method
int delayInt = differneceToAdd.intValue();
// here am subtracting that difference of time by 1 min so that I can
// show the notification before the scheduled time of post
int oneminuteBefore = delayInt - 60000;
// this is my method which takes the amount of difference in integer form
// and add that amount of time to the system's time,
// then on added time it shows the notification
scheduleNotification(getNotification("10 second delay"), oneminuteBefore);
这是我显示通知的方法:
private void scheduleNotification(Notification notification, int delay) {
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// here am adding that difference of time for scheduling notification
long futureInMillis = SystemClock.elapsedRealtime() + delay;
Log.v("ReminderTime = "+futureInMillis, "");
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
我不知道为什么我没有收到通知,但我知道我的方法工作正常,因为我尝试使用1min
,30sec
等的一些硬编码。
答案 0 :(得分:0)
事实证明我尝试过这样的事情非常简单
String[] postTypeArray = new String[]{"1 hour Before", "6 hour Before", "1 Day Before"};
new MaterialDialog.Builder(this)
.title("Set Reminder")
.items(postTypeArray)
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
if (which == 0) {
Calendar cal = Calendar.getInstance();
cal.setTime(postScheduledDate);
cal.add(Calendar.HOUR, -1);
Long oneHourBack = cal.getTimeInMillis();
scheduleNotification(getNotification( dateStr ,""+titleStr), oneHourBack);
Toast.makeText(getBaseContext(), "Reminder has been setted for " + text, Toast.LENGTH_SHORT).show();
} else if (which == 1) {
Calendar cal = Calendar.getInstance();
cal.setTime(postScheduledDate);
cal.add(Calendar.HOUR, -1);
Long oneHourBack = cal.getTimeInMillis();
scheduleNotification(getNotification( dateStr ,""+titleStr), oneHourBack);
Toast.makeText(getBaseContext(), "Reminder has been setted for " + text, Toast.LENGTH_SHORT).show();
} else if ( which == 2){
Calendar cal = Calendar.getInstance();
cal.setTime(postScheduledDate);
cal.add(Calendar.HOUR, -1);
Long oneHourBack = cal.getTimeInMillis();
scheduleNotification(getNotification( dateStr ,""+titleStr), oneHourBack);
Toast.makeText(getBaseContext(), "Reminder has been setted for " + text, Toast.LENGTH_SHORT).show();
}
}
})
.show();
此处MaterialDialog
是一个对话框构建器,假设它是一个listView,其中包含用于选择通知时间的不同选项