处理警报通知

时间:2018-09-01 03:04:38

标签: java android android-studio alarmmanager notificationmanager

我开始在Android上结合使用警报管理器和通知管理器,这是我的代码:

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setReminderAlarm();
}

public void setReminderAlarm() {
    Intent intent = new Intent(MainActivity.this, ReminderReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ReminderReceiver.NOTIF_ID, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 6);
    calendar.set(Calendar.MINUTE, 59);

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000L, pendingIntent);
}

ReminderReceiver.java

public static int NOTIF_ID = 101;

@Override
public void onReceive(Context context, Intent intent) {
    showAlarmNotification(context);
}

public void showAlarmNotification(Context context) {
    Intent intentClick = new Intent(context, MainActivity.class);
    PendingIntent pendingClick = PendingIntent.getActivity(context, 0, intentClick, 0);
    NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.cinema)
            .setContentTitle(title)
            .setContentIntent(pendingClick)
            .setContentText(message)
            .setColor(ContextCompat.getColor(context, android.R.color.transparent))
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
            .setSound(alarmSound)
            .setAutoCancel(true);
    notificationManagerCompat.notify(NOTIF_ID, builder.build());
}

每次我运行该应用程序时,即使设置了时间,该通知也会始终出现。我该如何预防? 谢谢。

1 个答案:

答案 0 :(得分:0)

问题是您将时间设置为当天。因此,如果您在6:00之后打开应用,则警报管理器将立即触发。

您需要检查当天的时间是否超过6:00,如果是,则需要将日期更改为第二天:

public void setReminderAlarm() {
    Intent intent = new Intent(MainActivity.this, ReminderReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ReminderReceiver.NOTIF_ID, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int curHr = calendar.get(Calendar.HOUR_OF_DAY);
    // Checking whether current hour is over 6
    if (curHr >= 6)
    {
        // Since current hour is over 6, setting the date to the next day
        calendar.add(Calendar.DATE, 1);
    }
    calendar.set(Calendar.HOUR_OF_DAY, 6);
    calendar.set(Calendar.MINUTE, 59);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000L, pendingIntent);
}