How to add calendar events to default calendar, silently without Intent, in android 4?

后端 未结 6 1156
迷失自我
迷失自我 2020-12-04 16:41

I want to add calendar events programmatically (directly) in android 4+. Is it this possible to be tested on emulator? I don\'t own an android phone. Some sample code wou

6条回答
  •  佛祖请我去吃肉
    2020-12-04 17:18

    Here is the way to ask user to which calendar the event has to be added. As my requirement was this and didn't find the solution at one place. I have summarized and came up with this solution, hope it helps someone :)

    final ContentResolver cr = this.getContentResolver();
            Cursor cursor ;
            if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "calendar_displayName" }, null, null, null);
            else
                cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
            if (cursor != null && cursor.moveToFirst() ) {
                final String[] calNames = new String[cursor.getCount()];
                final int[] calIds = new int[cursor.getCount()];
                for (int i = 0; i < calNames.length; i++) {
                    calIds[i] = cursor.getInt(0);
                    calNames[i] = cursor.getString(1);
                    cursor.moveToNext();
                }
    
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                final long startDate = sdf.parse(slotData.getSlot_date() + " " + slotData.getSlot_from()).getTime();
                final long endDate = sdf.parse(slotData.getSlot_date() + " " + slotData.getSlot_to()).getTime();
    
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Select any one");
                builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ContentValues cv = new ContentValues();
                        cv.put("calendar_id", calIds[which]);
                        cv.put("title", title);
                        cv.put("dtstart", startDate);
                        cv.put("hasAlarm", 1);
                        cv.put("dtend", endDate);
                        cv.put("eventTimezone", TimeZone.getDefault().getID());
    
                        Uri newEvent ;
                        if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                            newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
                        else
                            newEvent = cr.insert(Uri.parse("content://calendar/events"), cv);
    
                        if (newEvent != null) {
                            long id = Long.parseLong( newEvent.getLastPathSegment() );
                            ContentValues values = new ContentValues();
                            values.put( "event_id", id );
                            values.put( "method", 1 );
                            values.put( "minutes", 15 ); // 15 minutes
                            if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                                cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                            else
                                cr.insert( Uri.parse( "content://calendar/reminders" ), values );
    
                        }
                        dialog.cancel();
                    }
    
                });
    
                builder.create().show();
            }
            if (cursor != null) {
                cursor.close();
            }
    

提交回复
热议问题