这样做。
if (updateTime.getTimeInMillis() < System.currentTimeMillis()
|| updateTime.before(new GregorianCalendar())) {
updateTime.set(Calendar.HOUR_OF_DAY, hourOfDay + 24);
updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
}
并创建一个类来设置警报。
public class setAlarm {
Context context;
public setAlarm(Context c) {
// TODO Auto-generated constructor stub
this.context = c;
}
public void settingAlarm(Calendar calendar, int counter) {
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
counter, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
和报警广播接收器是公共类报警接收器扩展广播接收器{
private NotificationManager manager;
private int NOTIFICATION_ID;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm Received", Toast.LENGTH_LONG).show();
manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Food Time", System.currentTimeMillis());
Intent intent2 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "FoodTime",
"Click to View your food", pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = Uri
.parse("android.resource://"
+ "com.technogalax.sixsmallmealaday.alarm" + "/"
+ R.raw.nsound);
manager.notify(NOTIFICATION_ID, notification);
}
}