使用Google的日历API v3和android项目添加日历活动

时间:2016-12-22 08:34:47

标签: android google-calendar-api

    /* ####################################################################################################################################
                                            Calendar API
#################################################################################################################################### */

    private class InsertRequest extends AsyncTask<Void, Void, List<String>>{

        private com.google.api.services.calendar.Calendar AppService = null;
        private Exception AppLastError = null;

        InsertRequest(GoogleAccountCredential credentials){
            HttpTransport transport = AndroidHttp.newCompatibleTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

            AppService = new com.google.api.services.calendar.Calendar.Builder(
                    transport,jsonFactory,credentials
            ).setApplicationName("SmartStudy2").build();
        }
/**********************************************************************************************/
        @Override
        protected List<String> doInBackground(Void... voids) {
            try {
                return insertEventThroughAPI();
            } catch (Exception e){
                AppLastError = e;
                cancel(true);
                return null;
            }
        }
/**********************************************************************************************/
        private List<String> insertEventThroughAPI() throws Exception {

            /*DateTime now = new DateTime(System.currentTimeMillis());

            Event event = new Event()
                    .setSummary("jeremy event")
                    .setLocation("Panji goa")
                    .setDescription("test Event");
            EventDateTime start = new EventDateTime()
                    .setDateTime(now)
                    .setTimeZone("Asia/Kolkata");
            event.setStart(start);
            event.setEnd(start);
            String CalendarID = "primary";

            AppService.events().insert(CalendarID,event).execute();
            TextView tv = (TextView) findViewById(R.id.Tview1);
            tv.setText("hell yeah");*/

            Event event = new Event()
                    .setSummary("Google I/O 2015")
                    .setLocation("800 Howard St., San Francisco, CA 94103")
                    .setDescription("A chance to hear more about Google's developer products.");

            DateTime startDateTime = new DateTime("2016-12-28T09:00:00-07:00");
            EventDateTime start = new EventDateTime()
                    .setDateTime(startDateTime)
                    .setTimeZone("America/Los_Angeles");
            event.setStart(start);

            DateTime endDateTime = new DateTime("2016-12-31T17:00:00-07:00");
            EventDateTime end = new EventDateTime()
                    .setDateTime(endDateTime)
                    .setTimeZone("America/Los_Angeles");
            event.setEnd(end);

            String[] recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=2"};
            event.setRecurrence(Arrays.asList(recurrence));


            EventReminder[] reminderOverrides = new EventReminder[] {
                    new EventReminder().setMethod("email").setMinutes(24 * 60),
                    new EventReminder().setMethod("popup").setMinutes(10),
            };
            Event.Reminders reminders = new Event.Reminders()
                    .setUseDefault(false)
                    .setOverrides(Arrays.asList(reminderOverrides));
            event.setReminders(reminders);

            String calendarId = "primary";
            event = AppService.events().insert(calendarId, event).execute();
            System.out.printf("Event created: %s\n", event.getHtmlLink());




            return null;
        }
/**********************************************************************************************/
        @Override
        protected void onPreExecute(){
            TextView tv = (TextView) findViewById(R.id.Tview1);
            tv.setText("In Progress");
            (MainActivity.this).insertProgress.show();
        }

        @Override
        protected void onPostExecute(List<String> output){
            (MainActivity.this).insertProgress.hide();
            TextView tv = (TextView) findViewById(R.id.Tview1);
            if (output == null || output.size() == 0)
                tv.setText("no result");
            else
                tv.setText(TextUtils.join("",output));


        }

        @Override
        protected void onCancelled(){
            (MainActivity.this).insertProgress.hide();
            TextView tv = (TextView) findViewById(R.id.Tview1);

            if(AppLastError != null){
                if(AppLastError instanceof GooglePlayServicesAvailabilityIOException){
                    showGooglePlayPlayServicesAvailabilityErrorDialog(
                            ((GooglePlayServicesAvailabilityIOException) AppLastError).getConnectionStatusCode()
                    );
                }
                else if (AppLastError instanceof UserRecoverableAuthIOException){
                    startActivityForResult(
                            ((UserRecoverableAuthIOException) AppLastError).getIntent(),
                            MainActivity.REQUEST_AUTHORIZAION
                            );
                } else {
                    tv.setText("the following error occured \n:"+AppLastError.getMessage());
                }
            } else {
                tv.setText("you cancelled");
            }
        }

    }
  

此insert事件被取消并返回null异常   我正在进行所需的所有必要检查,如播放服务可用性和检查设备连接   我究竟做错了什么   我是按照谷歌日历API文档

完成的

PS:安卓日历提供商我不能替代我

0 个答案:

没有答案