How to read and edit Android calendar events using the new Android 4.0 Ice Cream Sandwich API?

前端 未结 6 2164
醉梦人生
醉梦人生 2020-11-28 22:13

We are trying to show user the Ice cream Sandwich calendar view, when they want to add a new event. We can only test this in emulator.

The other problem is that we c

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 22:39

    public static ArrayList readCalendarEvent(Context context) {
            Cursor cursor = context.getContentResolver()
                    .query(
                            Uri.parse("content://com.android.calendar/events"),
                            new String[] { "calendar_id", "title", "description",
                                    "dtstart", "dtend", "eventLocation" }, null,
                            null, null);
            cursor.moveToFirst();
            // fetching calendars name
            String CNames[] = new String[cursor.getCount()];
    
            // fetching calendars id
            nameOfEvent.clear();
            startDates.clear();
            endDates.clear();
            descriptions.clear();
            Log.d("cnameslength",""+CNames.length);
            if (CNames.length==0)
            {
             Toast.makeText(context,"No event exists in calendar",Toast.LENGTH_LONG).show();
            }
            for (int i = 0; i < CNames.length; i++) {
    
                nameOfEvent.add(cursor.getString(1));
                startDates.add(getDate(Long.parseLong(cursor.getString(3))));
                endDates.add(getDate(Long.parseLong(cursor.getString(4))));
                descriptions.add(cursor.getString(2));
                CNames[i] = cursor.getString(1);
                cursor.moveToNext();
                Log.d("datacur",""+nameOfEvent.get(i));
                Log.d("datacur",""+startDates.get(i));
                Log.d("datacur",""+endDates.get(i));
                Log.d("datacur",""+descriptions.get(i));
                String filename=nameOfEvent.get(i)+"::"+startDates.get(i)+"::"+endDates.get(i)+"::"+descriptions.get(i);
                generateNoteOnSD(context,nameOfEvent.get(i),filename);
    
            }
            return nameOfEvent;
        }
    
    
    
    
    
    public static void generateNoteOnSD(Context context, String sFileName, String sBody) {
            try {
                File root = new File(Environment.getExternalStorageDirectory(), "Notes");
                if (!root.exists()) {
                    root.mkdirs();
                }
                File gpxfile = new File(root, sFileName);
                FileWriter writer = new FileWriter(gpxfile);
                writer.append(sBody);
                writer.flush();
                writer.close();
                Toast.makeText(context, "Successfully Backup Created", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

提交回复
热议问题