如何设置报警经理火里的星期和时间的特定日期在Android的?如何设置、星期、日期、经理

2023-09-05 06:07:29 作者:絶版丶末末

例如我想有一个警报,将触发每个星期天中午....我将如何做到这一点?

for example i want to have an alarm that will fire every sunday at noon.... how would i do this?

推荐答案

使用的AlarmManager类:

Use the AlarmManager class:

http://developer.android.com/reference/android/app/AlarmManager.html

类概述

这个类提供了访问   系统报警服务。这允许你   安排您的应用程序运行   在未来的某个时候。当   警报响起,该份意向书   被登记为它是由广播   系统,自动启动   目标应用程序,如果是不   已经运行。注册警报   保留,而在设备处于睡眠   (而且可以选择唤醒设备   如果他们在这段时间熄灭),但   如果它被关闭将被清除   并重新启动。

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

使用公共无效集(int型的,长triggerAtTime,PendingIntent操作)来设定时间启动它。

Use public void set (int type, long triggerAtTime, PendingIntent operation) to set the time to fire it.

使用无效setRepeating(int型的,长triggerAtTime,间隔长,PendingIntent操作)来安排重复报警。

下面是一个完整的例子。我真的不记得所有的日历的方法,所以我敢肯定,这部分可以简化,但是这是一个开始,你可以在以后优化它:

Here's a full example. I don't really remember all the Calendar methods, so I'm sure that part can be streamlined, but this is a start and you can optimize it later:

AlarmManager alarm = (AlarmMAnager) Context.getSystemService(Context.ALARM_SERVICE);
Calendar timeOff = Calendar.getInstance();
int days = Calendar.SUNDAY + (7 - timeOff.get(Calendar.DAY_OF_WEEK)); // how many days until Sunday
timeOff.add(Calendar.DATE, days);
timeOff.set(Calendar.HOUR, 12);
timeOff.set(Calendar.MINUTE, 0);
timeOff.set(Calendar.SECOND, 0);

alarm.set(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(), yourOperation);