2011-05-13 95 views
1

您好我目前使用以下代码将事件添加到Android的默认日历。向日历添加提醒

Calendar cal = Calendar.getInstance();    
    Intent intent = new Intent(Intent.ACTION_EDIT); 
     intent.setType("vnd.android.cursor.item/event"); 
    intent.putExtra("beginTime", cal.getTimeInMillis()); 
    intent.putExtra("allDay", false); 
    intent.putExtra("rrule", "FREQ=DAILY"); 
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000); 
    intent.putExtra("title", "A Test Event from android app"); 
    intent.putExtra("description", "Who knows! this might work"); 
intent.putExtra("eventLocation", "Hope this time it works"); 
    intent.putExtra("hasAlarm", 1); 
    startActivity(intent); 

我的问题是, 正如你所看到的,我已经使用 “FREQ = DAILY”,同样也有,如 “FREQ = YEARLY” 和 “频率=月刊” 值。 我想知道其他可用的替代方案,以便我可以在我的代码中提供它们。

回答

0

哦。现在我懂了。我不得不将这添加到上面的代码。

 ContentResolver cr = getContentResolver(); 
    Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders"); 
    values = new ContentValues(); 
    values.put("event_id", Long.parseLong(event.getLastPathSegment())); 
    values.put("method", 1); 
    values.put("minutes", 5); 
    cr.insert(REMINDERS_URI, values); 
+0

这是什么额外的代码没有和手段?请添加解释p.s我知道它在一年前被问过! :)\ – shareef 2013-01-05 17:26:02

0

下面的代码为我工作:

//===========code to add event============================= 

          try { 
            Date mDate = sdf.parse(givenDateString); 

            long settime= mDate.getTime(); 

            ContentValues values = new ContentValues(); 
            values.put("calendar_id", 1); 
            values.put("title", "Daze Event"); 
            values.put("dtstart", settime); 
            values.put("dtend", settime+10000); 
            values.put("description", "daze event"); 
            values.put("hasAlarm", 1); 
            String timezone=TimeZone.getDefault().getID(); 
            values.put("eventTimezone", timezone); 
            Uri baseUri; 
            if (Build.VERSION.SDK_INT >= 8) { 

            baseUri = Uri.parse("content://com.android.calendar/events"); 
            } else { 
             baseUri = Uri.parse("content://calendar/events"); 
            } 
            Uri uri = getContentResolver().insert(Events.CONTENT_URI, values); 

            // getContentResolver().insert(baseUri, values); 
            long eventID = Long.parseLong(uri.getLastPathSegment()); 


//==========code to reminder set :================ 

             Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/reminders"); 
             values = new ContentValues(); 
             values.put("event_id", Long.parseLong(uri.getLastPathSegment())); 
             values.put("method", 1); 
             values.put("minutes", 1); 
             getContentResolver().insert(REMINDERS_URI, values); 

            } catch (Exception e) { 
             Log.d("catch", ""); 
             // Toast.makeText(getApplicationContext(), "id=", 45).show(); 
               e.printStackTrace(); 
            }