我正在开发一个Android应用程序,我想在14:30每天发送通知。我能按时收到通知,但我的问题是,每当我在14:30之后打开应用程序时,我每次都会收到通知。怎么解决? 发送通知的代码在这里......!
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 30);
Intent intent = new Intent(getApplicationContext(), TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100,intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
和 TimeAlarm.class 的代码是....
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context, MainActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.ic_btn_speak_now)
.setContentTitle("Title")
.setContentText("Text")
.setAutoCancel(true);
manager.notify(100,builder.build());
答案 0 :(得分:0)
试试这个:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.ic_btn_speak_now)
.setContentTitle("Title")
.setContentText("Text")
.setOngoing(false)
.setWhen(calendar.getTimeInMillis());
.setAutoCancel(true);
manager.notify(100,builder.build());
NotificationCompat.Builder setWhen (long when)
设置事件发生的时间。面板中的通知按此时间排序。
您甚至可以尝试setShowWhen
控制是否在内容视图中显示使用setWhen设置的时间戳。