CalendarProvider,如何获取会议的所有者姓名?
CalendarProvider, how to get meeting's owner name?
我需要从 CalendarProvider 获取会议的所有者姓名。
我找到了这两行:
CalendarContract.Events.ORGANIZER,
CalendarContract.Events.OWNER_ACCOUNT
但他们 return 只有创建者的电子邮件。
如何获取创作者的姓名?
尝试使用以下代码获取事件详细信息。
有关详细信息,您可以查看 google android 日历文档
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);
}
}
}
当你查询到所有需要的事件后,当你在 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;
}
我需要从 CalendarProvider 获取会议的所有者姓名。 我找到了这两行:
CalendarContract.Events.ORGANIZER,
CalendarContract.Events.OWNER_ACCOUNT
但他们 return 只有创建者的电子邮件。
如何获取创作者的姓名?
尝试使用以下代码获取事件详细信息。 有关详细信息,您可以查看 google android 日历文档 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);
}
}
}
当你查询到所有需要的事件后,当你在 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;
}