我有一个场景
设置闹钟(周一到周五)
假设我选择的时间是:hour = 9, minutes = 15, am_pm = "AM"
。
现在我想为每个星期一到星期五上午 9:15 设置闹钟
下面的代码我试过了,但没有得到想要的结果。
if(choice.equals("Week Days (Mon-Fri)"))
{
for(int a = 2; a <= 5; a++) //here I am assuming a is from 2 to 5 (calendar DAY_OF_WEEK from Monday to Friday)
{
Calendar alarmCalendar = Calendar.getInstance();
alarmCalendar.set(Calendar.HOUR_OF_DAY, _hourOfDay);
alarmCalendar.set(Calendar.MINUTE, _minute);
alarmCalendar.set(Calendar.SECOND, 0);
alarmCalendar.set(Calendar.MILLISECOND, 0);
if(am_pm.equals("AM"))
{
alarmCalendar.set(Calendar.AM_PM, 0);
}
else
{
alarmCalendar.set(Calendar.AM_PM, 1);
}
alarmCalendar.set(Calendar.DAY_OF_WEEK, a);
Long alarmTime = alarmCalendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
alarmTime, 24 * 60 * 60 * 1000 , pendingIntent);
}
Toast.makeText(ActivityReminder.this, "Meeting Reminder Set on Week Days (Mon-Fri)",
Toast.LENGTH_LONG).show();
}
我使用过 BroadcastReceiver
,例如:
public class AlarmReceiver extends BroadcastReceiver
{
NotificationManager mNotificationManager;
Context context;
public static final String TAG = "Reminder...";
@Override
public void onReceive(Context context, Intent intent)
{
this.context = context;
String subject = "<h3>Meeting Reminder: </h3>" + intent.getStringExtra("subject");
Toast.makeText(context, Html.fromHtml(subject), Toast.LENGTH_LONG).show();
showNotification(subject);
}
@SuppressWarnings("deprecation")
private void showNotification(String msg)
{
Intent notificationIntent = new Intent(context.getApplicationContext(),
ActivityMainScreen.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ActivityMainGeofence.class);
stackBuilder.addNextIntent(notificationIntent);
String arrivalTime = TimeUtil.toString(Calendar.getInstance().getTime(),
"dd-MM-yyyy hh:mm:ss a");
notificationIntent.putExtra("subject", msg)
.putExtra("time", arrivalTime).putExtra("type", "Meetings/Reminder");
PendingIntent notificationPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification();
notification.icon = R.drawable.app_icon;
notification.setLatestEventInfo(context.getApplicationContext(),
"WFM Meeting Reminder", Html.fromHtml(msg), notificationPendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notification);
}
}
最后在 list 中:
<receiver android:name="com.my_package_name.AlarmReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
已编辑:
在这里,我想说明 One Time
闹钟对我有用,但对于上述选择,Week Days (Mon-Fri)
无法正常工作
工作代码是:
if(choice.equals("One Time"))
{
alarmManager.set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); // here PERIOD is the time selected by me in milliseconds...
Toast.makeText(ActivityReminder.this, "Meeting Reminder Set One Time",
Toast.LENGTH_LONG).show();
}
我哪里做错了。任何帮助将不胜感激。
最佳答案
我会假设闹钟只设置在星期五的 9:15?这应该是因为 AlarmManager 文档中的以下行:
如果已经为同一个 IntentSender 安排了一个警报,它将首先被取消。
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int , 长, 长, android.app.PendingIntent)
为了做你想做的事,你要么需要 5 个 PendingIntents,要么只为第一个事件设置一个警报,当收到该警报时,你可以为第二天设置警报,依此类推。
我可能会选择第二个选项,因为在第一个方法中您需要 5 个不同的 PendingIntents,这意味着它们的 requestCode 或支持 Intent 必须不同(具有不同的类型、操作或类别)。
关于android - 如何在android中为工作日设置闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26859959/