标签:
这是我在做项目的时候总结的一些关于添加事件到系统日历的经验,希望对大家有所帮助,下面主要针对重复添加事件到日历进行分析,其中需要注意的事项。
但是在Android2.2版本之后,三个URL变成了如下:
Calendar cal = Calendar.getInstance();
args.put("dtstart", data.getDateTime() - 1);
String tt=Utils.getDateFmtFromTime(data.getDateTime());
args.put("dtend", data.getDateTime());
args.put("title", data.getRemindName());
args.put("description", data.getRemindName());
args.put("needStartApplication", false);
String result = CalendarUtil.addEvent(this, args);//添加到日历
args.put("dtend",entity.getEndTakeMedicineDate());
args.put("dtstart", entity.getStartTakeMedicineDate());
int days=ConvertUtil.compareDate(entity.getStartTakeMedicineDate(), entity.getEndTakeMedicineDate());
int countTime=(entity.getTakeMedicineCycle()+1)*days;
switch (i) {
case 0:
args.put("rrule", String.format("FREQ=YEARLY;COUNT=%s;BYHOUR=%s;BYMINUTE=%s",countTime,entity.getFirstTakeMedicineTime()[0],entity.getFirstTakeMedicineTime()[1]));
break;
case 1:
args.put("rrule", String.format("FREQ=YEARLY;COUNT=%s;BYHOUR=%s;BYMINUTE=%s",countTime,entity.getSecondTakeMedicineTime()[0],entity.getSecondTakeMedicineTime()[1]));
break;
case 2:
args.putcishi String.format("FREQ=YEARLY;COUNT=%s;BYHOUR=%s;BYMINUTE=%s",countTime,entity.getThirdTakeMedicineTime()[0],entity.getThirdTakeMedicineTime()[1]));
break;
default:
break;
}
args.put(Reminders.MINUTES, 5);
args.put("title", entity.getMedicineName());
args.put("description", String.format("%s%s%s%s",getString(R.string.the_one_day_take_medicine),entity.getTakeMedicineCycle()+1,getString(R.string.the_amount_of_time),entity.getNumOfEachTime()));
args.put("needStartApplication", false);
for (int i = 0; i <entity.getTakeMedicineCycle()+1; i++) {
JSONObject args=new JSONObject();
//TODO 构建标准参数
try {
long startTime=entity.getStartTakeMedicineDate();//开始的日期
long endTime= entity.getEndTakeMedicineDate();//结束 的日期
switch (i) {
case 0:
//把开始提醒的日期和时间用Calendar组合起来
Calendar startTakeTime1=Calendar.getInstance();
startTakeTime1.setTimeInMillis(startTime);
startTakeTime1.set(Calendar.HOUR, entity.getFirstTakeMedicineTime()[0]);//开始的时间点
startTakeTime1.set(Calendar.MINUTE, entity.getFirstTakeMedicineTime()[1]);//开始的分钟
Calendar endTakeTime1=Calendar.getInstance();
endTakeTime1.setTimeInMillis(endTime);
endTakeTime1.set(Calendar.HOUR, entity.getFirstTakeMedicineTime()[0]);
endTakeTime1.set(Calendar.MINUTE, entity.getFirstTakeMedicineTime()[1]);
args.put("dtstart", startTakeTime1.getTimeInMillis());
args.put("dtend",endTakeTime1.getTimeInMillis());
args.put("dtend",endTakeTime1.getTimeInMillis());
break;
case 1:
Calendar startTakeTime2=Calendar.getInstance();
startTakeTime2.setTimeInMillis(startTime);
startTakeTime2.set(Calendar.HOUR, entity.getSecondTakeMedicineTime()[0]);
startTakeTime2.set(Calendar.MINUTE, entity.getSecondTakeMedicineTime()[1]);
Calendar endTakeTime2=Calendar.getInstance();
endTakeTime2.setTimeInMillis(endTime);
endTakeTime2.set(Calendar.HOUR, entity.getSecondTakeMedicineTime()[0]);
endTakeTime2.set(Calendar.MINUTE, entity.getSecondTakeMedicineTime()[1]);
args.put("dtstart", startTakeTime2.getTimeInMillis());
args.put("dtend",endTakeTime2.getTimeInMillis());
break;
case 2:
String thrid=String.format("%s:%s", entity.getThirdTakeMedicineTime()[0],entity.getThirdTakeMedicineTime()[1]);
Calendar startTakeTime3=Calendar.getInstance();
startTakeTime3.setTimeInMillis(startTime);
startTakeTime3.set(Calendar.HOUR, entity.getThirdTakeMedicineTime()[0]);
startTakeTime3.set(Calendar.MINUTE, entity.getThirdTakeMedicineTime()[1]);
Calendar endTakeTime3=Calendar.getInstance();
endTakeTime3.setTimeInMillis(endTime);
endTakeTime3.set(Calendar.HOUR, entity.getThirdTakeMedicineTime()[0]);
endTakeTime3.set(Calendar.MINUTE,entity.getThirdTakeMedicineTime()[1]);
args.put("dtstart", startTakeTime3.getTimeInMillis());
args.put("dtend",endTakeTime3.getTimeInMillis());
break;
default:
break;
}
args.put(Reminders.MINUTES, 5);//提醒5分钟有提醒
args.put("title", entity.getMedicineName());
args.put("description", String.format("%s%s%s%s",getString(R.string.the_one_day_take_medicine),entity.getTakeMedicineCycle()+1,getString(R.string.the_amount_of_time),entity.getNumOfEachTime()));
args.put("needStartApplication", false);
//添加事件到日历
String result=CalendarUtil.addEvent(context, args);
}
/**
* 增加事件到日历
*
* @param context
* @param args
* 参数
*/
public static String addEvent(Context context, JSONObject args) {
String result = null;
if(args!=null){
long calendarId = getCalendarId(context);
if (calendarId == -1) {
// no calendar account; react meaningfully
return null;
}
ContentValues values = new ContentValues();
long startTime=System.currentTimeMillis();
if(args.has("dtstart")){
startTime = Long.valueOf(JSONUtil.getJSONObjectValue(args, "dtstart").toString());
values.put("dtstart", startTime);
}
long endTime=-1;
if (args.has("dtend")) {
endTime = Long.valueOf(JSONUtil.getJSONObjectValue(args, "dtend").toString());
values.put("dtend", endTime);
}
// 添加事件
if(args.has("rrule")){
String id=Events._ID;
values.put("rrule", JSONUtil.getJSONObjectValue(args, "rrule").toString());
}
if(args.has("duration")){
values.put("duration", JSONUtil.getJSONObjectValue(args, "duration").toString());
}
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
values.put(Events.EVENT_END_TIMEZONE, TimeZone.getDefault().getID());
if(args.has("title")){
values.put("title", JSONUtil.getJSONObjectValue(args, "title").toString());
}
values.put(Events.CALENDAR_ID, calendarId);
if(args.has("description")){
values.put("description",JSONUtil.getJSONObjectValue(args,"description").toString());
}
Uri uri = context.getContentResolver().insert(Uri.parse(calanderEventURL), values);
long eventId = Long.valueOf(uri.getLastPathSegment());
values.clear();
// 添加提醒
values.put(Reminders.EVENT_ID, eventId);
if(args.has(Reminders.MINUTES)){
String id=Reminders._ID;
values.put(Reminders.MINUTES,JSONUtil.getJSONObjectValue(args, Reminders.MINUTES).toString());
}
values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
Uri reminderUri = context.getContentResolver().insert(Uri.parse(calanderRemiderURL), values);
long reminderId = Long.valueOf(reminderUri.getLastPathSegment());
if(reminderId>0){
if(args.has("needStartApplication")){
boolean needStartApplication=Boolean.valueOf(JSONUtil.getJSONObjectValue(args, "needStartApplication").toString());
if(needStartApplication){
FZZSApplication.getInstance().toOtherActivity(context, Constants.TO_REMIND_TO_SPLASH_ACTIVITY, endTime);
}
}
try {
JSONObject resultObj=new JSONObject();
resultObj.put("calendarId", calendarId);
resultObj.put("eventId", eventId);
resultObj.put("reminderId", reminderId);
result=resultObj.toString();//把三个Id返回
} catch (Exception e) {
}
}
}
return result;
}
标签:
原文地址:http://www.cnblogs.com/sissy/p/4521770.html