从用户Google日历获取事件的最佳做法是什么?

时间:2019-06-04 09:19:34

标签: android kotlin google-calendar-api

我想从用户的Google日历中读取事件,并在Android应用中将事件的标题用于将来用途。

到目前为止,我已经阅读了有关google-calendar-api和Calendar-provider的文档,但是无法运行任何“入门”指南。我也找不到任何有关如何实现api的有用示例。

我知道此过程(用户需要使用其Google帐户进行身份验证,并可以使用该信息来访问所需的内容),但是正如我所说,我不知道如何在android应用程序中实现此功能。

如果有人有有用的代码,示例或任何建议,我将不胜感激,因为这个简单的话题使我发疯。

1 个答案:

答案 0 :(得分:0)

使用内容提供者,您可以访问日历事件,内容解析器query()方法用于获取内容提供者数据。您需要传递需要检索的列的列表。

您可以从android日历文档中获取参考,请检查下面的链接https://developer.android.com/reference/java/util/Calendar

  // add below permission in your manifest file and ask these permission at run time also
   <uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
   <uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>


  public void getDataFromEventsTable() {
    Cursor cur = null;
    ContentResolver cr =getContentResolver();

    //if you want access calendar details use below uri
   // Uri uri = CalendarContract.Calendars.CONTENT_URI;
    String[] calmProjection ={CalendarContract.Calendars._ID,
                    CalendarContract.Calendars.ACCOUNT_NAME,
                    CalendarContract.Calendars.ACCOUNT_TYPE,
                    CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
                    CalendarContract.Calendars.CALENDAR_COLOR,
                    CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
                    CalendarContract.Calendars.OWNER_ACCOUNT,
                    CalendarContract.Calendars.CALENDAR_TIME_ZONE,
                    CalendarContract.Calendars.ALLOWED_REMINDERS};

    //if your want access event related info use below uri
    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 ("
            + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?) AND ("
            + CalendarContract.Calendars.OWNER_ACCOUNT + " = ?))";
  // pass here details of account, which account linked with your google calendar 
 String[] selectionArgs = new String[]{"xyz@gmail.com", "com.google",
            "xyz@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));
            Log.d("Events", "->" + eventTitle + "->" + eventId + "->" + startDate + "->" + endDate);
        }
        }
}