1
我为我的应用程序构建了通知。它的工作原理是,它在正确的时间推动,并且在按下时链接到正确的活动。通知不重复
但是,我把它称为重复闹钟,因为我希望它在特定的日子里拍摄出来。 在我最初的测试中,我将其设置为每5秒推一次,以便快速检查它是否正确重复。在最初推送后,一旦我清除它,通知便不会再出现。
这里是我的代码在我的主要活动,以建立alarmManager:
private void notificationAlarm() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, 1);
cal.set(Calendar.HOUR, 1);
cal.set(Calendar.MINUTE, 40);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long interval = cal.getTimeInMillis()+5000;
Intent alarmIntent = new Intent(this, alarmNotif.class);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager notifAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//notifAlarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent);
notifAlarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), interval, alarmPendingIntent);
}
和我的广播接收器中的代码:
公共类alarmNotif扩展广播接收器{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = "Don't forget to order sushi from Arbuckle!";
String subTitle = "Order before 10 AM with Arbuckle App";
Intent notifIntent = new Intent(context, SecureAppStarter.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(subTitle)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
Notification notif = notifBuilder.getNotification();
notifManager.notify(1, notif);
}
}
有一点帮助吗?没有任何消息就能唤醒这个停泊。浪费了5个小时的睡眠! – Laurent