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

后端 未结 6 1165
迷失自我
迷失自我 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:15

    Using this code, you can programmatically add an event to device calendar. I have tested in Marshmallow, and it works fine for me.

    private void addToDeviceCalendar(String startDate,String endDate, String title,String description, String location) {
    
            String stDate = startDate;
            String enDate = endDate;
    
            GregorianCalendar calDate = new GregorianCalendar();
            //GregorianCalendar calEndDate = new GregorianCalendar();
    
            SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy,MM,dd,HH,mm");
            Date date,edate;
            try {
                date = originalFormat.parse(startDate);
                stDate=targetFormat.format(date);
    
            } catch (ParseException ex) {}
    
            long startMillis = 0;
            long endMillis = 0;
            String dates[] = stDate.split(",");
    
            SD_YeaR = dates[0];
            SD_MontH = dates[1];
            SD_DaY = dates[2];
            SD_HouR = dates[3];
            SD_MinutE = dates[4];
    
    
            /*Log.e("YeaR ", SD_YeaR);
            Log.e("MontH ",SD_MontH );
            Log.e("DaY ", SD_DaY);
            Log.e(" HouR", SD_HouR);
            Log.e("MinutE ", SD_MinutE);*/
    
            calDate.set(Integer.parseInt(SD_YeaR), Integer.parseInt(SD_MontH)-1, Integer.parseInt(SD_DaY), Integer.parseInt(SD_HouR), Integer.parseInt(SD_MinutE));
            startMillis = calDate.getTimeInMillis();
    /*
            try {
                edate = originalFormat.parse(endDate);
                enDate=targetFormat.format(edate);
    
            } catch (ParseException ex) {}
    
    
            String end_dates[] = endDate.split(",");
    
            String ED_YeaR = end_dates[0];
            String ED_MontH = end_dates[1];
            String ED_DaY = end_dates[2];
    
            String ED_HouR = end_dates[3];
            String ED_MinutE = end_dates[4];
    
    
            calEndDate.set(Integer.parseInt(ED_YeaR), Integer.parseInt(ED_MontH)-1, Integer.parseInt(ED_DaY), Integer.parseInt(ED_HouR), Integer.parseInt(ED_MinutE));
            endMillis = calEndDate.getTimeInMillis();*/
    
            try {
                ContentResolver cr = getActivity().getContentResolver();
                ContentValues values = new ContentValues();
                values.put(CalendarContract.Events.DTSTART, startMillis);
                values.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis() + 60 * 60 * 1000);
                values.put(CalendarContract.Events.TITLE, title);
                values.put(CalendarContract.Events.DESCRIPTION, description);
                values.put(CalendarContract.Events.EVENT_LOCATION,location);
                values.put(CalendarContract.Events.HAS_ALARM,1);
                values.put(CalendarContract.Events.CALENDAR_ID, 1);
                values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
                        .getTimeZone().getID());
                System.out.println(Calendar.getInstance().getTimeZone().getID());
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
    
                    return;
                }
                Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
    
                long eventId = Long.parseLong(uri.getLastPathSegment());
                Log.d("Ketan_Event_Id", String.valueOf(eventId));
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

提交回复
热议问题