如何在一周和时间的特定日期创建多个计划通知



我需要创建多个通知,每个通知都有一周中的某一天在特定时间显示。 例如,每周一晚上 8 点,当单击通知时,它应该转到我的应用程序中的活动。我还有一个设置活动,用户可以在其中选择他们想要显示或不显示的通知。

我已经查看了大量示例甚至 android 文档,但还没有找到一种方法来使其工作。

提前谢谢。

由于系统对后台处理的不同限制,确切的时间可能相当困难。但一个令人满意的解决方案可能是使用警报管理器。

https://developer.android.com/training/scheduling/alarms#examples-of-real-time-clock-alarms

摘自上述文档

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 24 hours.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 60 * 24, alarmIntent);

您必须创建并注册警报接收器

public class AlarmReceiver extends BroadcastReceiver
{    
public AlarmReceiver (){}
public void onReceive(Context context, Intent intent)
{
doSomething();
}
}

在活动中的某个位置注册接收器

private AlarmReceiver receiver = new AlarmReceiver();
this.registerReceiver(this.receiver, new IntentFilter());

最新更新