我如何设置一个闹钟管理器,每一个星期的具体日子和时间在android



例如,我想有一个警报,将在每周日中午触发....我该怎么做呢?

使用AlarmManager类

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

类概述

这个类提供对系统告警服务。这些可以让你安排要运行的应用程序在未来的某个时候。当一个警报响起,意图已登记为广播电台系统自动启动如果不是目标应用程序已经运行。已注册的告警有在设备休眠时保留(并且可以选择唤醒设备如果在这段时间内爆炸的话),但是关闭后会清空吗和重启。

使用public void set (int type, long triggerAtTime, PendingIntent operation)设置点火时间

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

这里有一个完整的例子。我真的不记得所有的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);

最后这个正确的解决方案如果设置为(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);
} 

最新更新