所以我做了一个应该在每个星期三早上8:15发送通知的应用程序。我正在使用AlarmManager从日历日期发送不精确的重复警报,以打开发送通知的待处理意图。
目前正在发生的事情是,只有在您首次下载应用时才会发送通知。今天早上它没有像我自己或我的任何用户那样出现。我想知道AlarmManager是否在第一次通知触发后的一周内被安排(即首次下载应用程序后一周)?
以下是我在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
这是挂起的意图调用的我的AlarmReceiver:
公共类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
}
}
这是我的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);
}
}
}
答案 0 :(得分:2)
首先,您正在使用setInexactRepeating()
。除非您的minSdkVersion
为19或更高,否则您无法在任意时段内使用setInexactRepeating()
。
其次,setInexactRepeating()
不准确。你的闹钟不会在星期三早上8:15响起。它会在某个时候消失。引用the JavaDocs:
您的闹钟的第一次触发不会在请求的时间之前,但在此之后几乎整整一段时间内可能不会发生。
第三,正如Squonk在评论中指出的那样,您使用RTC
作为警报类型。您的警报将被进一步延迟,直到设备因其他原因被唤醒。
如果targetSdkVersion
低于19,则可以使用setRepeating()
进行精确的重复闹铃。否则,使用set()
(API级别19级)和setExact()
(API级别19+)来安排警报,并在警报响起时安排下一次警报作为完成工作的一部分。此外,如果您想将设备从睡眠模式唤醒以完成工作,请使用RTC_WAKEUP
。