Java 立即触发本地通知
我使用以下代码在android中触发本地通知Java 立即触发本地通知,java,android,Java,Android,我使用以下代码在android中触发本地通知 AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION"); notificationIn
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(NewPaymentReminder.this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, Integer.parseInt(Month_only));
cal.set(Calendar.YEAR, Integer.parseInt(Year_only));
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(Date_only));
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
// cal.set(Calendar.SECOND, secs);
cal.set(Calendar.AM_PM, Calendar.PM);
System.out.println("Calender - Get time in Milliseconds" + cal.getTimeInMillis());
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
接收器类别:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, MyPaymentReminder.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MyPaymentReminder.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Payment Reminder")
.setContentText("New Notification")
.setTicker("New Message Alert!")
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
我已经在清单中注册了我的接收者,也在receiver类中收到了数据。但我的问题是我想在特定的日期和时间触发通知,但它会立即触发!朋友们,任何帮助都将不胜感激 检查是否
System.out.println("Calender - Get time in Milliseconds" + cal.getTimeInMillis());
是印刷时代的未来
此外,如果目标ApiLevel高于19,则应使用alarmManager.setExact: 我已经将时间触发时间设置为04:29,并且cal.getTimeInMillis()返回一个值-61622604035261。它返回一个负值DrakCheck如果你设置的是正确的年份,据我所知你设置的是0017年而不是2017年我发现solution@Drake,而不是上面,我只是以秒为单位找到两个日期之间的差异,并将其发送到alarm manager!现在,它的工作很好!谢谢@Drake!