设置警报管理器以触发多个通知

Setting up Alarm Manager to trigger multiple notifications

在我的应用程序中,我想每天执行某项任务,并且在该任务完成后我想在不同时间触发多个通知(这些时间将在执行该任务时计算)。 谷歌搜索后我发现使用 AlarmManager 是我最好的选择:

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

我的问题是: 1.通知仅在第一次显示,之后不再显示。 2. 每次我重新启动该应用程序时都会触发 AlarmManager 并显示过去的通知。

PS.: 我是 android 的新手,感谢任何帮助

这是我尝试过的:

我的 activity 中的功能来自我正在设置此警报:

    private void handleNotification() {

    Intent alarmIntent = new Intent(this, AlarmReciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

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

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

这是我的 AlarmReciever class:

public class AlarmReciever extends BroadcastReceiver {

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

    ArrayList<String> times = new ArrayList();
    GPSTracker gpsTracker = new GPSTracker(context);

    //calculate the times arraylist

    DateFormat format;
    Date date = null;
    for (String s : times) {
        if (hour12) {
            format = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
            try {
                date = format.parse(s);
            } catch (ParseException e) {
            }
        } else {
            format = new SimpleDateFormat("hh:mm", Locale.ENGLISH);
            try {
                date = format.parse(s);
            } catch (ParseException e) {
            }
        }
        Intent alarmIntent = new Intent(context, NotificationReciever.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Log.d(Constants.LOG_TAG, calendar.getTime().toString());

        if (Build.VERSION.SDK_INT < 19) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }
    }
}

}

这是我的 NotificationReciever class:

public class NotificationReciever extends BroadcastReceiver {

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

    if (Constants.D) Log.d(Constants.LOG_TAG, "NOTIFICATION RECIEVER");

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.sc_logo_final)
                    .setContentTitle(context.getResources().getString(R.string.app_name))
                    .setContentText(context.getResources().getString(R.string.app_name));
    Intent resultIntent = new Intent(context, NavigationDrawerActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(NavigationDrawerActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}

}

在我的 AndroidManifest.xml,

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

        <receiver android:name=".AlarmReciever">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".NotificationReciever">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

首先,这段代码很可能设置了在当天午夜发生的闹钟,几乎可以肯定过去与当前时间相比,这意味着您会很快收到警报:

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

根据 setRepeating() 的 javadoc:

If the stated trigger time is in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.

您的闹钟不会重复,因为您正在使用标志 INTERVAL_DAY, which only works with setInexactRepeating()

如果您想 运行 在 天的午夜做某事,您需要在日历中添加一天。您也可以考虑将秒字段也设置为 0。

如果您在每次应用程序启动时都注册闹钟,那么您每次都会收到早期闹钟。同样根据 javadoc:

If there is already an alarm scheduled for the same IntentSender, it will first be canceled.

警报没有重复,因为您正在经过一个仅对 setInexactRepeating().

有效的时间间隔