我想在手机的日历上添加提醒功能。换句话说,提醒需要在电话的真实日历中。我们可以使用AlarmManager创建提醒,但我希望它出现在日历中。我如何在Android中制作它?
答案 0 :(得分:4)
在按钮点击中添加此代码或在任何地方添加提醒日历
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
try
{
epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
//epoch=epoch;
Log.e("epoch",String.valueOf(epoch));
epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
//epoch1=epoch1;
Log.e("epoch1",String.valueOf(epoch1));
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
values.put("calendar_id", 1);
values.put("title", "Appoitment");
values.put("allDay", 0);
values.put("dtstart",epoch); // event starts at 11 minutes from now
values.put("dtend", epoch1 ); // ends 60 minutes from now
values.put("description", "Your consulting date and time ");
values.put("visibility", 0);
values.put("hasAlarm", 1);
if(EVENTS_URI!=null)
{
event1 = cr.insert(EVENTS_URI, values);
}
// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event1.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
if(REMINDERS_URI!=null)
{
cr.insert( REMINDERS_URI, values );
}
alertDialog.setTitle("Event Saved");
Dismiss();
alertDialog.show();
在代码中添加“getCalendarUriBase”函数。
private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
注意:
epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
在这行代码中,日期和时间必须是您要添加remindar的日期和时间。
权限:
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />