我必须创建calendarview和Add事件。此添加事件与Android设备日历同步。意思是,使用Android设备日历中的CalendarView Show添加事件。如何在Android设备日历中显示此事件。
如何进行此活动15分钟前发出提醒。
我在清单文件中提到了权限:
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
下面的代码为“读取和写入日历”权限提供了错误。
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
String date = i2 +" / " + (i1+1) + " / " + i;
showAlertDialog(date);
}
});
permission();
}
public void showAlertDialog(final String date){
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.alert_dialog_item, null);
final EditText etTitle =(EditText)dialogView.findViewById(R.id.etTitle);
final EditText etEvent = (EditText)dialogView.findViewById(R.id.etEvent);;
final EditText etEventDate = (EditText)dialogView.findViewById(R.id.etDate);;
final EditText etTime = (EditText)dialogView.findViewById(R.id.etTime);;
// eventDate.setText((new StringBuilder().append(day).append("/").append(month+1).append("/").append(year)));
etEventDate.setText(date);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Add Event");
builder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String title = etTitle.getText().toString();
String event = etEvent.getText().toString();
String date = etEventDate.getText().toString();
String time = etTime.getText().toString();
calendarEvent(title, event,date,time);
try {
String eventUriString = "content://com.android.calendar/events";
ContentValues eventValues = new ContentValues();
eventValues.put("calendar_id", 1); // id, We need to choose from
// our mobile for primary its 1
eventValues.put("title", title);
eventValues.put("description", event);
//long endDate = Long.parseLong(date + 1000 * 10 * 10); // For next 10min
eventValues.put("dtstart", date);
eventValues.put("time", time);
eventValues.put("eventTimezone", "UTC/GMT +5:30");
eventValues.put("hasAlarm", 1); // 0 for false, 1 for true
uri = getApplicationContext()
.getContentResolver()
.insert(Uri.parse(eventUriString), eventValues);
long eventID = Long.parseLong(uri.getLastPathSegment());
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
builder.setCancelable(true);
}
});
builder.show();