我有 sqlite 数据库表,它与朋友及其联系人的出生日期联系。我想在数据库中的任何日期与当前日期匹配时发出警报。请任何人帮助我如何完成这项任务。实际上我想每天匹配日期 2 次,请建议我。
我正在使用此代码
private void setAlarm(String name,String date)
{
String[] arrdob =date.split("/");
Calendar cal = Calendar.getInstance(); //for using this you need to import java.util.Calendar;
// add minutes to the calendar object
cal.set(Calendar.MONTH,Integer.parseInt(arrdob[1]));
cal.set(Calendar.YEAR, Integer.parseInt(arrdob[2]));
cal.set(Calendar.DAY_OF_MONTH,Integer.parseInt(arrdob[0]));
cal.set(Calendar.HOUR_OF_DAY,14);
cal.set(Calendar.MINUTE,35);
System.out.println("1");
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmintent.putExtra("title","B'Day Alarm");
alarmintent.putExtra("subject","Hi!Todays "+name+" B'Day");
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
//VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to
//AlarmReceiver Class
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
在将值插入数据库时调用此方法。AlarmReceiver 类如下:
private static int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent intent) //
{
// NotificationManager mNotificationManager =
System.out.println("2");
//context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification =
new Notification(R.drawable.ic_launcher, "Combi Note",System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, AlarmReceiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("title");
//here we get the title and description of our Notification
String note=extras.getString("subject");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//这里我们为 //notification 设置默认声音
// The PendingIntent to launch our activity if the user selects this notification
manger.notify(NOTIFICATION_ID++, notification);
}