Android 如何在我的应用程序中的特定时间获取每日通知?

Android 如何在我的应用程序中的特定时间获取每日通知?,android,notifications,android-push-notification,Android,Notifications,Android Push Notification,我正在开发一个应用程序,在这个应用程序中,我必须在特定的时间获得每日通知,我如何编写代码? 假设我必须在每天早上8:00收到通知,当我单击该通知时,它必须打开我的应用程序。有一个叫做报警管理器的东西,你可以在这里阅读: 或者某种计时器,您可以使用此代码来安排重复报警 AlarmManager alarmMgr; PendingIntent alarmIntent; alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SE

我正在开发一个应用程序,在这个应用程序中,我必须在特定的时间获得每日通知,我如何编写代码?
假设我必须在每天早上8:00收到通知,当我单击该通知时,它必须打开我的应用程序。

有一个叫做报警管理器的东西,你可以在这里阅读:


或者某种计时器,您可以使用此代码来安排重复报警

AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
// Set the alarm to start at 8:00 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);

Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// setRepeating() lets you specify a precise custom interval--in this case,
// one day.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);

这仅设置报警,当系统触发此报警时,您需要在广播接收器中接收此报警并从广播接收器发出通知,即在本例中,AlarmReceiver.class

是,但并非如文档中所述,setRepeating将从API级别19设置不精确报警注:自API 19起,所有重复报警均不精确。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,并按上述方式重新安排每次的时间。targetSdkVersion早于API 19的传统应用程序将继续将其所有报警(包括重复报警)视为精确报警。