2013-04-15 61 views
0

您好我正在開發一個Android應用程序,我在其中處理日曆。我無法在自己的日曆中添加活動。我使用了下面的代碼。使用代碼我能夠在默認日曆添加事件..請建議我在我的代碼在我的應用程序想要添加活動日曆到Android日曆

private void addevent() { 
     // TODO Auto-generated method stub 
     Intent calIntent = new Intent(Intent.ACTION_INSERT); 
     calIntent.setType("vnd.android.cursor.item/event"); 
     calIntent.putExtra(Events.TITLE, "My House Party"); 
     calIntent.putExtra(Events.EVENT_LOCATION, "My Beach House"); 
     calIntent.putExtra(Events.DESCRIPTION, "A Pig Roast on the Beach"); 
     GregorianCalendar calDate = new GregorianCalendar(2013, 4, 16); 
     calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 
     calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
      calDate.getTimeInMillis()); 
     calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
      calDate.getTimeInMillis()); 
     startActivity(calIntent); 
     Toast.makeText(this, "event added", Toast.LENGTH_SHORT).show(); 

    } 

回答

3

的日曆添加事件,如果在下面的代碼壓延使用添加事件所需的變化,即在我的日曆中工作。

//此代碼用於在日曆中添加事件。

ContentResolver cr = getContentResolver(); 
    ContentValues values = new ContentValues(); 
    values.put(Events.ALL_DAY, Events.ALL_DAY_VALUE); 
    values.put(Events.DTSTART, startTimeInMilli); 
    values.put(Events.DTEND, endTimeInMilli); 
    values.put(Events.TITLE, strTaskName); 
    values.put(Events.DESCRIPTION, strDescription); 
    values.put(Events.CALENDAR_ID, 0); 
    values.put(Events.VISIBILITY, Events.VISIBILITY_VALUE); 

    Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events"); 
    Uri uri = cr.insert(EVENTS_URI, values); 
    long eventID = Long.parseLong(uri.getLastPathSegment()); 

//此函數調用上層代碼。

private String getCalendarUriBase(Activity act) { 

    String calendarUriBase = null; 
    Uri calendars = Uri.parse("content://calendar/calendars"); 
    Cursor managedCursor = null; 
    try { 
     managedCursor = act.managedQuery(calendars, null, null, null, null); 
    } catch (Exception e) { 
    } 
    if (managedCursor != null) { 
     calendarUriBase = "content://calendar/"; 
    } else { 
     calendars = Uri.parse("content://com.android.calendar/calendars"); 
     try { 
      managedCursor = act.managedQuery(calendars, null, null, null, 
        null); 
     } catch (Exception e) { 
     } 
     if (managedCursor != null) { 
      calendarUriBase = "content://com.android.calendar/"; 
     } 
    } 
    return calendarUriBase; 
} 

//其他靜態類事件常量。

public class Events { 

public static String ALL_DAY = "allDay"; 
public static String DTSTART = "dtstart"; 
public static String DTEND = "dtend"; 
public static String TITLE = "title"; 
public static String DESCRIPTION = "description"; 
public static String CALENDAR_ID = "calendar_id"; 
public static String EVENT_TIMEZONE = "eventTimezone"; 

public static String VISIBILITY = "visibility"; 
public static String HASALARM = "hasAlarm"; 

public static int VISIBILITY_VALUE = 0; 
public static int HASALARM_VALUE = 1; 
public static int ALL_DAY_VALUE = 0; 

} 
+0

managedQuery()是一個過時函數N給出的錯誤u能請提一些變化那邊 –

+0

這錯誤來了? 在我的代碼沒有錯誤managedQuery()函數,我設置minSdkVersion =「7」和測試星系的工作完美。並在日曆中添加活動。 –

+0

其賦予的力量關閉,同時添加事件n在managedCursor = act.managedQuery(日曆,null,null,null, null)中顯示異常; –

0

你需要你的應用程序的通日曆ID在你的意圖putExtra。

calIntent.putExtra(Events.CALENDAR_ID , my_cal_id); 

要獲得設備中所有可用的日曆,你可以使用下面的代碼清單:

if(android.os.Build.VERSION.SDK_INT 
         >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) 
    cursor = getContentResolver().query(Calendars.CONTENT_URI,new String[] 
      { Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME }, null ,null, null); 
else 
    cursor = getContentResolver().query(Uri.parse("content://com.android.calendar 
    /calendars"),new String[] { "_id", "displayName" }, "selected=1",null, null); 

     if (cursor != null && cursor.moveToFirst()) { 
      String[] calNames = new String[cursor.getCount()]; 
      int[] calIds = new int[cursor.getCount()]; 
      for (int i = 0; i < calNames.length; i++) { 

       calIds[i] = cursor.getInt(0); 
       calNames[i] = cursor.getString(1); 
       System.out.println(""+cursor.getInt(0) + " -- "+ 
                  cursor.getString(1)); 
       cursor.moveToNext(); 
      } 
      cursor.close();