java 我如何设置闹钟管理器在 android 中的每个特定日期和时间触发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5601678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:51:06  来源:igfitidea点击:

how do i set an alarm manager to fire every on specific day of week and time in android?

javaandroidalarmmanager

提问by waa1990

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

例如,我想要一个警报,它会在每个星期天的中午响起……我该怎么做?

回答by Aleadam

Use the AlarmManager class:

使用 AlarmManager 类:

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

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

Class Overview

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.

课程概览

此类提供对系统警报服务的访问。这些允许您安排您的应用程序在未来的某个时间运行。当警报响起时,系统会广播为其注册的 Intent,如果目标应用程序尚未运行,则会自动启动它。当设备处于睡眠状态时,注册的警报会保留(如果它们在这段时间内关闭,可以选择唤醒设备),但如果关闭并重新启动,则会被清除。

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

使用public void set (int type, long triggerAtTime, PendingIntent operation)设定的触发它的时间。

Use void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)to schedule a repeating alarm.

使用void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)调度重复报警。

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:

这是一个完整的例子。我不太记得所有的 Calendar 方法,所以我确定可以精简这部分,但这是一个开始,您可以稍后对其进行优化:

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);

回答by Mo'nes Qasaimeh

finally this right solution if set as (sun,tus ,fri) you must create three alarm for these three day the following code set alarm every sunday and send dayOfWeek=1;

最后这个正确的解决方案如果设置为 (sun,tus,fri) 你必须为这三天创建三个警报下面的代码设置警报每个星期天并发送 dayOfWeek=1;

 public void setAlarm_sun(int dayOfWeek) {
     cal1.set(Calendar.DAY_OF_WEEK, dayOfWeek);
     Toast.makeText(getApplicationContext(), "sun "+cal1.get(Calendar.DAY_OF_WEEK), 222).show();

     Toast.makeText(getApplicationContext(), "Finsh", 222).show();

        Intent intent = new Intent(this, SecActivity.class);
        PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0,
                intent, 0);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

         Long alarmTime = cal1.getTimeInMillis();
         AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);

       // am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,7* 24 * 60 * 60 * 1000 , pendingIntent);
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,7* 24 * 60 * 60 * 1000 , pendingIntent);

}