Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在日期之前设置通知_Java_Android - Fatal编程技术网

Java 在日期之前设置通知

Java 在日期之前设置通知,java,android,Java,Android,我想根据日期(日期为PostScheduledDate)安排通知,通知将在PostScheduledDate之前,并且在我的代码中,之前的时间量为1分钟 我试过这个: //here am converting the postScheduledDate to milliseconds Long postScheduledLong = postScheduledDate.getTime(); // and here converting system time to millisecond Lo

我想根据日期(日期为
PostScheduledDate
)安排通知,通知将在
PostScheduledDate
之前,并且在我的代码中,
之前的时间量为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);
}

我不知道为什么我没有收到通知,但我知道我的方法工作得很好,因为我用一些硬代码尝试了
1分钟
30秒
等等。

结果很简单,我尝试了类似的方法

 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
是一个对话框生成器,假设它是一个列表视图,具有选择通知时间的不同选项

什么是NotificationPublisher?它的类作为receiver@logcat进行响应