我需要从CalendarProvider获取会议的所有者名称。 我发现这两行:
CalendarContract.Events.ORGANIZER,
CalendarContract.Events.OWNER_ACCOUNT
但是他们只返回创建者的电子邮件。
如何获取创作者的姓名?
答案 0 :(得分:0)
请尝试以下代码以获取事件详细信息。 有关更多详细信息,您可以查看google android calendar docs https://developer.android.com/guide/topics/providers/calendar-provider
// add below permission in your app manifest file and also ask these two permission at run time
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
// add below method in your activity
public void getOrganizerName() {
Cursor cur = null;
ContentResolver cr =getContentResolver();
Uri uri=CalendarContract.Events.CONTENT_URI;
String[] eventmProjection =
{CalendarContract.Calendars._ID,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.TITLE,
CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Events.ORGANIZER,
CalendarContract.Events._ID,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.DURATION,
CalendarContract.Events.SYNC_DATA1,
CalendarContract.Events.DIRTY,
CalendarContract.Events.UID_2445,
CalendarContract.Events.DELETED,
CalendarContract.Events.LAST_DATE,
CalendarContract.Events.SYNC_DATA2,
CalendarContract.Events.ALL_DAY,
CalendarContract.Events.RRULE,
CalendarContract.Events.STATUS,
CalendarContract.Events.RDATE
};
String selection = "(" + CalendarContract.Calendars.ACCOUNT_NAME + " = ? AND deleted != 1 AND " + CalendarContract.Events.ORGANIZER + " != ?)";
// pass here google calendar sync account detials
String[] selectionArgs = new String[]{"abc@gmail.com","abc@gmail.com"};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR},12);
}
cur = cr.query(uri, eventmProjection, selection, selectionArgs, null);
if(cur.getCount()>0){
while (cur.moveToNext()) {
String eventTitle = cur.getString(cur.getColumnIndex(CalendarContract.Events.TITLE));
String eventId = cur.getString(cur.getColumnIndex(CalendarContract.Events._ID));
String startDate = cur.getString(cur.getColumnIndex(CalendarContract.Events.DTSTART));
String endDate = cur.getString(cur.getColumnIndex(CalendarContract.Events.DTEND));
String org_name=cur.getString(cur.getColumnIndex(CalendarContract.Events.ORGANIZER));
Log.d("Events", "->" + eventTitle + "->" + eventId + "->" + startDate + "->" + endDate+ "->"+org_name);
}
}
}
答案 1 :(得分:0)
查询完所有必需的事件后,当您处于Cursor循环中时,对于每个事件,您都可以使用以下方法获得名称:
private String getOrganizerNameFromAttendees(int eventId, String organizerMail) {
Logger.e(TAG, "getOrganizerNameFromAttendees");
final String[] args = new String[]{String.valueOf(eventId), organizerMail};
final Cursor cursor = mApplicationContext.getContentResolver().query(CalendarContract.Attendees.CONTENT_URI, ATTENDEE_PROJECTION, ATTENDEE_SELECTION, args, null);
String name = null;
try {
while (cursor.moveToNext()) {
String nameFromAttendees = cursor.getString(ATTENDEE_PROJECTION_NAME_INDEX);
String mailFromAttendees = cursor.getString(ATTENDEE_PROJECTION_MAIL_INDEX);
name = TextUtils.isEmpty(nameFromAttendees) ? mailFromAttendees : nameFromAttendees;
}
} catch (Exception e) {
Logger.e(TAG, "getOrganizerNameFromAttendees, exception = " + e.getMessage());
} finally {
if (cursor != null) {
cursor.close();
}
}
return name;
}