简体   繁体   English

在请求时不触发AlarmManager

[英]AlarmManager not firing when requested

So I've made an app that's supposed to send a notification at 8:15 every Wednesday morning. 因此,我制作了一个应用程序,该应用程序应该在每个星期三上午8:15发送通知。 I'm using an AlarmManager to send inexact repeating alarms from a calendar date to open a pending intent that sends a notification. 我正在使用AlarmManager从日历日期发送不精确的重复警报,以打开发送通知的未决意图。

What's currently happening is the notification is being sent only if you first download the app. 当前正在发生的情况是仅在您首次下载该应用程序时才发送通知。 It did not go off this morning like it should have for myself or any of my users. 今天早晨,它并没有像我自己或我的任何用户那样出现。 I'm wondering if the AlarmManager is being scheduled for one week after that first notification fires (ie, a week after first downloading the app)? 我想知道是否将AlarmManager安排在发出第一个通知后的一周内(即,第一次下载该应用程序后的一周内)?

Here is my manageNotifications method in MainActivity: 这是MainActivity中我的manageNotifications方法:

public void manageNotifications() {

    alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarmIntent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    calendar = java.util.Calendar.getInstance();

    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(java.util.Calendar.DAY_OF_WEEK, java.util.Calendar.WEDNESDAY);
    calendar.set(java.util.Calendar.HOUR_OF_DAY, 8);
    calendar.set(java.util.Calendar.MINUTE, 15);

    /*
    sets alarm manager to go off at 8:15 in the morning every 7 days on Wednesday
     */
    alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24 * 7, pendingIntent);
}//end manageNotifications

Here is my AlarmReceiver that the pending intent calls: 这是我的AlarmReceiver,即将进行的意图调用:

public class AlarmReceiver extends BroadcastReceiver { 公共类AlarmReceiver扩展了BroadcastReceiver {

NotificationCompat.Builder notificationBuilder;
Intent notificationResultIntent;
ArrayList<Show> shows;
SharedPreferences spSpreadsheets, spNotifications;
final String showSpreadsheetURL = "https://docs.google.com/spreadsheets/d/1Ax2-gUY33i_pRHZIwR8AULy6-nbnAbM8Qm5-CGISevc/gviz/tq";

public void onReceive(Context context, Intent intent) {
    System.out.println("AlarmReceiver created");
    spNotifications = context.getSharedPreferences("notificationToggle", Context.MODE_PRIVATE);
    spSpreadsheets = context.getSharedPreferences("spreadsheets", Context.MODE_PRIVATE);

    if (spNotifications.getBoolean("notifications", false)) {
        int nextRegularShowIndex = 0, i = 0;

        shows = new ArrayList<>();

        try {
            if (spSpreadsheets.getString("showsSpreadsheet", "").equals(""))
                getShows(context);
            shows = processShowsJson(new JSONObject(spSpreadsheets.getString("showsSpreadsheet", "")));

            while (shows.get(i).getShowTime() != 0) {
            /*
            checks for first instance of a regular showtime
             */
                i++;
                nextRegularShowIndex = i;
            }
            checkForPastShows();
            notificationBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.applogo)
                    .setContentTitle("Comedy Club of Jacksonville")
                    .setContentText(shows.get(nextRegularShowIndex).getComedian() + " headlines this weekend at the Comedy " +
                            "Club of Jacksonville.  Click to read more.")
                    .setDefaults(Notification.DEFAULT_ALL);
            notificationResultIntent = new Intent(context, ThisWeekendFromNotification.class).putParcelableArrayListExtra("shows", shows);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(ThisWeekend.class);
            stackBuilder.addNextIntent(notificationResultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(resultPendingIntent);
            notificationBuilder.setAutoCancel(true);
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(0, notificationBuilder.build());
            System.out.println("Notification built");

        } catch (Exception e) {
            e.printStackTrace();
        }//end onReceive
    }
}

And here is my BootReceiver class used to reset the alarm when a phone is restarted: 这是我的BootReceiver类,用于在电话重启时重置警报:

public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(java.util.Calendar.DAY_OF_WEEK, java.util.Calendar.WEDNESDAY);
        calendar.set(java.util.Calendar.HOUR_OF_DAY, 8);
        calendar.set(java.util.Calendar.MINUTE, 15);

    /*
    sets alarm manager to go off at 8:15 in the morning every 7 days on Wednesday
     */
        alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24 * 7, pendingIntent);
    }
}

} }

First, you are using setInexactRepeating() . 首先,您正在使用setInexactRepeating() Unless your minSdkVersion is 19 or higher, you cannot use setInexactRepeating() with an arbitrary period. 除非您的minSdkVersion为19或更高,否则您不能在任意期间使用setInexactRepeating()

Second, setInexactRepeating() is inexact. 其次, setInexactRepeating()不精确。 Your alarm will not go off at 8:15am on a Wednesday. 您的闹钟不会在星期三的上午8:15响起。 It will go off sometime. 它会在某个时候熄灭。 Quoting the JavaDocs : 引用JavaDocs

Your alarm's first trigger will not be before the requested time, but it might not occur for almost a full interval after that time. 警报的第一个触发器不会在请求的时间之前出现,但是在该时间之后的几乎整个时间间隔内都不会发生。

Third, as Squonk notes in the comment, you are using RTC as the alarm type. 第三,正如Squonk在评论中指出的那样,您正在使用RTC作为警报类型。 Your alarm will be further delayed, until the device wakes up for some other reason. 您的闹钟将进一步延迟,直到设备由于其他原因唤醒。

If your targetSdkVersion is below 19, you can use setRepeating() for an exact repeating alarm. 如果targetSdkVersion低于19,则可以将setRepeating()用于精确的重复警报。 Otherwise, use set() (pre-API Level 19) and setExact() (API Level 19+) to schedule your alarm, and schedule the next alarm as part of the work done when the alarm goes off. 否则,请使用set() (API级别19之前)和setExact() (API级别19+)来计划您的警报,并计划下一个警报作为警报关闭时所做工作的一部分。 Also, use RTC_WAKEUP , if you want to wake the device out of sleep mode to do your work. 另外,如果您想将设备从睡眠模式中唤醒,请使用RTC_WAKEUP进行工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM