2012-01-18 18 views
11

में Google कैलेंडर के साथ प्रोग्रामेटिक रूप से जोड़ा गया है, मैं एंड्रॉइड कैलेंडर में एक ईवेंट जोड़ने की कोशिश कर रहा हूं, और मैं निर्दिष्ट करता हूं कि Google कैलेंडर स्वचालित रूप से सिंक करने के लिए ईवेंट जीमेल कैलेंडर में जोड़ा जाएगा। समस्या प्रोग्राम प्रोग्रामिक रूप से Google कैलेंडर के साथ सिंक नहीं किया गया है, लेकिन अगर मैं इसे फोन पर मैन्युअल जोड़ता हूं तो यह Google कैलेंडर के साथ सिंक करता है। मुझे नहीं पता क्यों।सिंक इवेंट एंड्रॉइड

इस कोड है कि मैं घटना जोड़ने के लिए उपयोग करते हैं:

ArrayList<MyCalendar> calendars = new ArrayList<MyCalendar>(); 

String[] projection = new String[] { "_id", "name" }; 
Uri calUri = getCalendarURI(false); 
Cursor managedCursor = managedQuery(calUri, projection, "selected=1", 
     null, null); 

String calName = null; 
String calId = null; 
if (managedCursor.moveToFirst()) { 

    int nameColumn = managedCursor.getColumnIndex("name"); 
    int idColumn = managedCursor.getColumnIndex("_id"); 
    do { 
     calName = managedCursor.getString(nameColumn); 
     calId = managedCursor.getString(idColumn); 
     calendars.add(new MyCalendar(Integer.parseInt(calId), calName)); 
    } while (managedCursor.moveToNext()); 

} 
Toast.makeText(getBaseContext(), calName + " " + calId, 
     Toast.LENGTH_LONG).show(); 

Calendar cal = Calendar.getInstance(); 
ContentValues event = new ContentValues(); 
event.put("calendar_id", 2); 
event.put("title", "Test Event2"); 
event.put("description", "Hiii Buddy"); 
long startTime = cal.getTimeInMillis(); 
long endTime = cal.getTimeInMillis() + 60 * 60 * 1000; 
event.put("dtstart", startTime); 
event.put("dtend", endTime); 
event.put("allDay", 0); 
event.put("eventStatus", 1);// tentative 0, confirmed 1 canceled 2 
event.put("visibility", 3);// default 0 confidential 1 private 2 
          // public 3 
event.put("transparency", 0);// opaque 0 transparent 1 
event.put("hasAlarm", 1); // 0 false, 1 true 

Uri eventsUri = getCalendarURI(true); 
Uri url = getContentResolver().insert(eventsUri, event); 

तो ईवेंट सफलतापूर्वक कैलेंडर में शामिल कर, लेकिन यह वेब पर Google कैलेंडर में दिखाई नहीं देता (सिंक नहीं है) लेकिन अगर मैं मैन्युअल रूप से ईवेंट जोड़ता हूं तो यह सिंक हो जाता है !!!

+0

@ मैं एक ही समस्या है । क्या आपने अपनी समस्या हल की है ??? – asish

+2

क्या आपके पास अभी तक कोई समाधान है? – mvieghofer

उत्तर

2

गूगल कैलेंडर के साथ ही एंड्रॉयड कैलेंडर में एक घटना को सम्मिलित करने के लिए इस कोड का प्रयास करें:

ContentValues values = new ContentValues(); 

    cal_id = String.valueOf(p1); 
    values.put("calendar_id", p1); 
    values.put("title", title1); 
    values.put("allDay", 0); 
    values.put("dtstart", settime); 
    values.put("dtend", cal.getTimeInMillis()+60*60*1000); 
    values.put("description", desc1); 
    values.put("visibility", 0); 
    values.put("transparency", 0); 
    values.put("hasAttendeeData", 1); 
    values.put("hasAlarm", 0); 
    event = cr.insert(EVENTS_URI, values); 
    event1=event; 
    dat1 = event.toString(); 
    long id=-1; 
    if (event != null) 
    { 
     id = Long.parseLong(event.getLastPathSegment()); 
     ContentValues values1 = new ContentValues(); 
     values1.put("event_id", id); 
     values1.put("method", 1); //METHOD_ALERT 

     Uri reminder = Uri.parse(getCalendarUriBase(this) + "reminders"); 
     this.getContentResolver().insert(reminder, values1); 
     if(s.length() > 0 || partmail.length() > 0) 
     { 
    //REQUIRES FOLLOWING CODE 

इस कोड को घटना गूगल कैलेंडर

   ContentValues attendees = new ContentValues(); 
      attendees.put("event_id", id); 
      attendees.put("attendeeEmail", partmail1); 
      attendees.put("attendeeRelationship", 2);//RELATIONSHIP_ATTENDEE 
      attendees.put("attendeeStatus", 3); //ATTENDEE_STATUS_INVITED  
      attendees.put("attendeeType", 1); //TYPE_REQUIRED 

      id1=(int)id; 
      alarmid = (int) id; 
      Uri attendeesUri = null; 
      if (Integer.parseInt(Build.VERSION.SDK) >= 8) 
      { 
        attendeesUri = Uri.parse("content://com.android.calendar/attendees"); 
      } 
      else if(Integer.parseInt(Build.VERSION.SDK) < 8) 
      { 
        attendeesUri = Uri.parse("content://calendar/attendees"); 
      } 
      this.getContentResolver().insert(attendeesUri, attendees); 

      Toast.makeText(this, "Task Scheduled Successfully", Toast.LENGTH_SHORT).show(); 
     } 
     else 
     { 
      Toast.makeText(this, "Could not create Task!", Toast.LENGTH_SHORT);    
     } 


    // reminder insert 
    Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders"); 
    values = new ContentValues(); 
    values.put("event_id", id); 
    values.put("method", 1); 
    values.put("minutes", 0); 
    cr.insert(REMINDERS_URI, values); 

को sync'ed जोड़ने के लिए प्रयोग किया जाता है कैलेंडर की उरी प्राप्त करें क्योंकि यह एपीआई स्तरों के लिए अलग है जैसे कि 8 तक, 8 से अधिक और 11 से अधिक

private String getCalendarUriBase(Context con) { 
String calendarUriBase = null; 
Uri calendars = Uri.parse("content://calendar/calendars"); 
Cursor managedCursor = null; 
try { 
    managedCursor = managedQuery(calendars, null, null, null, null); 
} catch (Exception e) { 
    // eat 
} 

if (managedCursor != null) { 
    calendarUriBase = "content://calendar/"; 
} else { 
    calendars = Uri.parse("content://com.android.calendar/calendars"); 
    try { 
     managedCursor = managedQuery(calendars, null, null, null, null); 
    } catch (Exception e) { 
     // statement to print the stacktrace 
    } 

    if (managedCursor != null) { 
     calendarUriBase = "content://com.android.calendar/"; 
    } 

} 

return calendarUriBase; 

}

+0

समस्या इवेंट जोड़ने के साथ नहीं है। ईवेंट को स्थानीय कैलेंडर में जोड़ा गया है और Google कैलेंडर की घटना के रूप में दिखाया गया है, लेकिन Google कैलेंडर में नहीं। – asish

+0

@ashish: मेरा अद्यतन उत्तर –

+1

अभी भी काम नहीं कर रहा है .. – asish

1

उपयोग गूगल कैलेंडर एपीआई प्रदान की here

उपरोक्त संदर्भ में कुछ हद तक सामान्य है एंड्रॉयड एक अधिकारी एपीआई प्रदान नहीं करता है के रूप में। तो, एक और समाधान अधिक एंड्रॉइड विशिष्ट An Android Tutorial–Programming with Calendar

जिसमें एक कार्य उदाहरण भी शामिल है। इसके अलावा, WRITE_CALENDAR

READ_CALENDAR तरह अनुमति घोषणा पर सावधान रहना
3

आप इस समारोह से इसे जोड़ने के बाद अपने घटना सिंक कर सकते हैं, यह (एपीआई 8 में और बाद में) मेरे लिए काम किया है:

public static void syncCalendar(Context context, String calendarId) { 
     ContentResolver cr = context.getContentResolver(); 
     ContentValues values = new ContentValues(); 
     values.put(CalendarContract.Calendars.SYNC_EVENTS, 1); 
     values.put(CalendarContract.Calendars.VISIBLE, 1); 

     cr.update(
       ContentUris.withAppendedId(getCalendarUri(), 
         Long.parseLong(calendarId)), values, null, null); 
    } 
संबंधित मुद्दे