以编程方式将事件添加到日历 [英] Add events to Calendar programmatically

查看:89
本文介绍了以编程方式将事件添加到日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Marshmellow设备中的以下代码以编程方式将事件添加到日历,但无法正常工作。任何的想法?我无法在日历应用中看到此事件。

I'm using below code in Marshmellow device to add the event to the Calendar programmatically but it's not working. Any idea? I cannot see this event in the Calendar app.

        long startMillis = 0;
        long endMillis = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null, endDate = null;
        try{
            startDate = simpleDateFormat.parse("2017-05-01 01:30:00");
            startMillis = startDate.getTime();
            endDate = simpleDateFormat.parse("2017-05-01 03:30:00");
            endMillis = endDate.getTime();
        }catch (ParseException e){
            e.printStackTrace();
        }

        ContentResolver cr = this.getContentResolver();
        ContentValues values = new ContentValues();
        TimeZone timeZone = TimeZone.getDefault();
        values.put(CalendarContract.Events.DTSTART, startMillis);
        values.put(CalendarContract.Events.DTEND, endMillis);
        values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
        values.put(CalendarContract.Events.TITLE, "Hello Title");
        values.put(CalendarContract.Events.DESCRIPTION, "Add events to Calendar");
        values.put(CalendarContract.Events.CALENDAR_ID, 879);
        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

PS:如果只有一个gmail帐户与日历应用程序同步,则可以使用此功能。

推荐答案

我还发现了很多解决方案,最后我得到了解决方案,并成功地将日历事件与Schedule事件一起添加。

Am also find so many solutions finally i got solution and successfully calendar event added with Schedule event.

 Step -1 > Enable Google calnedar API From google console.
 Step - 2 > Add the permission in Androidmanifest.xml 

           <uses-permission android:name="android.permission.WRITE_CALENDAR"/> 

 Step =3 > Add library for checking Permissions in your  .gradle

          compile 'com.karumi:dexter:4.1.0'




    Dexter.withActivity(YoutActivityName.this)
                    .withPermission(Manifest.permission.WRITE_CALENDAR)
                    .withListener(new PermissionListener() 
                    {
                        @Override
                        public void onPermissionGranted(PermissionGrantedResponse response) {
                            try {

                                int calenderId = -1;
                                String calenderEmaillAddress = "sathishmicit2012@gmail.com";
                                String[] projection = new String[]{
                                        CalendarContract.Calendars._ID,
                                        CalendarContract.Calendars.ACCOUNT_NAME};
                                ContentResolver cr = context.getContentResolver();
                                Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection,
                                        CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" +
                                                CalendarContract.Calendars.NAME + "=? or " +
                                                CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)",
                                        new String[]{calenderEmaillAddress, calenderEmaillAddress,
                                                calenderEmaillAddress}, null);

                                if (cursor.moveToFirst()) {

                                    if (cursor.getString(1).equals(calenderEmaillAddress)) {

                                        calenderId = cursor.getInt(0);
                                    }
                                }


                                long start2 = Calendar.getInstance().getTimeInMillis(); // 2011-02-12 12h00
                                long end2 = Calendar.getInstance().getTimeInMillis() + (4 * 60 * 60 * 1000);   // 2011-02-12 13h00

                                String title = "This is my demo test with alaram with 5 minutes";

                                ContentValues cvEvent = new ContentValues();
                                cvEvent.put("calendar_id", calenderId);
                                cvEvent.put(CalendarContract.Events.TITLE, title);

                                cvEvent.put(CalendarContract.Events.DESCRIPTION, String.valueOf(start2));
                                cvEvent.put(CalendarContract.Events.EVENT_LOCATION, "Bhatar,Surat");
                                cvEvent.put("dtstart", start2);
                                cvEvent.put("hasAlarm", 1);
                                cvEvent.put("dtend", end2);

                                cvEvent.put("eventTimezone", TimeZone.getDefault().getID());


                                Uri uri = getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), cvEvent);


     // get the event ID that is the last element in the Uri  

                                long eventID = Long.parseLong(uri.getLastPathSegment());


                                ContentValues values = new ContentValues();

                                values.put(CalendarContract.Reminders.MINUTES, 2);
                                values.put(CalendarContract.Reminders.EVENT_ID, eventID);
                                values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALARM);
                                cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
                                //Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);


                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                        }

                        @Override
                        public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}

                        @Override
                        public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
                    }).check();

这篇关于以编程方式将事件添加到日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆