AlarmManager for Notifications的放置位置

时间:2016-06-27 13:01:36

标签: android alarmmanager android-notifications

我正在构建一个应用程序,我需要每隔12小时运行一次检查并相应地发出通知。为此,我使用了一个每12小时重复一次的AlarmManager,并在满足条件时发出通知:

MainActivity(在 onCreate 方法内):

//Set Notifications AlarmManager
AlarmManager AM = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent (this, NotificationAlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.setTimeInMillis(cal.getTimeInMillis() + 5000); //5 Seconds from now
AM.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), AlarmManager.INTERVAL_HALF_DAY , pending); //12 Hour Interval

通知功能正常,但是,我认为将AlarmManager置于onCreate方法中会出错,因为现在每次用户打开应用程序时都会重置AlarmManager。但是,我不知道在哪里可以启动它只能创建一次,而不是每次打开主Activity时都重复创建。

我觉得它与Service有关,但我还不知道如何使用服务,所以我不太确定

2 个答案:

答案 0 :(得分:1)

  

设置闹钟时,应检查是否存在闹钟。如果不   现有的警报然后创建新的警报。这样可以省去你   创建多个警报。

private void setAlarmIfRequired() {

        Intent notificationIntent = new Intent(this, NotificationAlarmReceiver.class);
        final PendingIntent pendingIntent =
                PendingIntent.getBroadcast(this, 0, notificationIntent,
                        PendingIntent.FLAG_NO_CREATE);
        if (pendingIntent == null) {
            // There is no alarm set for same
            //Set Notifications AlarmManager
            AlarmManager AM = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
            PendingIntent pending = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());
            cal.setTimeInMillis(cal.getTimeInMillis() + 5000); //5 Seconds from now
            AM.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), AlarmManager.INTERVAL_HALF_DAY , pending); //12 Hour Interval
        }
    }

在上面的示例中,我使用了您发布的问题代码。

Update1:​​最好是将此方法移动到实用程序类并将其作为静态方法,因此您可以从Activity或接收设备启动的接收器中调用它。

代码就像

public static void setAlarmIfRequired(Context context) {

        Intent notificationIntent = new Intent(context, NotificationAlarmReceiver.class);
        final PendingIntent pendingIntent =
                PendingIntent.getBroadcast(this, 0, notificationIntent,
                        PendingIntent.FLAG_NO_CREATE);
        if (pendingIntent == null) {
            // There is no alarm set for same
            //Set Notifications AlarmManager
            AlarmManager AM = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            PendingIntent pending = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());
            cal.setTimeInMillis(cal.getTimeInMillis() + 5000); //5 Seconds from now
            AM.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), AlarmManager.INTERVAL_HALF_DAY , pending); //12 Hour Interval
        }
    }

答案 1 :(得分:0)

我通常会对某个事件发出警报,因此它取决于用户交互,而不是活动生命周期。例如,当用户按下按钮时,我做了。

如果您不想让他或她启动闹钟,那么您必须使用服务并仅在活动尚未运行时才从活动中调用它。

请参阅https://developer.android.com/reference/android/app/Service.html

您的闹钟将有自己的生命周期,不会被重置。