Добавить событие в режиме просмотра календаря.когда добавить событие в календарь, то укажите ниже Ошибка разрешения:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 from ProcessRecord{3620f5c 13430:google.com/u0a149} (pid=13430, uid=10149) requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR
Дайте разрешение в файле манифеста Android.:
Мой код указан ниже:
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);
}
});
}
public void showAlertDialog(final String dateString1) {
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.etStartDate);
//final EditText etTime = (EditText) dialogView.findViewById(R.id.etTime);
final EditText etEndDate = (EditText) dialogView.findViewById(R.id.etEndDate);
Calendar cal = Calendar.getInstance();
long date1 = cal.getTimeInMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy, HH:mm a");
String dateString = dateFormat.format(date1);
etEventDate.setText(dateString);
long date2 = cal.getTimeInMillis() + 1000 * 60 * 60;
String endDate = dateFormat.format(date2);
etEndDate.setText(endDate);
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 eventDescription = etEvent.getText().toString();
String startDate = etEventDate.getText().toString();
String endDate = etEndDate.getText().toString();
//ContactsContract.CommonDataKinds.Event event = new ContactsContract.CommonDataKinds.Event(Color.RED,startDate,eventDescription);
try {
cr = getApplicationContext().getContentResolver();
cv = new ContentValues();
cv.put(CalendarContract.Events.DTSTART, startDate);
cv.put(CalendarContract.Events.DTEND, endDate);
cv.put(CalendarContract.Events.TITLE, title);
cv.put(CalendarContract.Events.DESCRIPTION, eventDescription);
cv.put(CalendarContract.Events.CALENDAR_ID, 1);
cv.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC/GMT+5:30");
cv.put(CalendarContract.Events.CALENDAR_COLOR_KEY, Color.RED);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, cv);
if (ActivityCompat.checkSelfPermission(Event.this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(Event.this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
}else if(ActivityCompat.checkSelfPermission(Event.this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(Event.this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED){
// ContentResolver contentResolver = getContentResolver();
//ContentValues contentValues = new ContentValues();
long eventID = Long.parseLong(uri.getLastPathSegment());
Toast.makeText(Event.this, (int) eventID, Toast.LENGTH_SHORT).show();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
builder.setCancelable(true);
}
});
builder.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_CALENDAR:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, " Permission is Granted", Toast.LENGTH_SHORT).show();
} else {
//code for deny
}
break;
}
}
как дать разрешение на чтение и запись для добавления события. Пожалуйста, направьте мне это разрешение.
Как добавить событие в синхронизацию календаря с мобильным календарем.