2013-03-22 72 views
0

我是android开发新手。我正在处理日历应用程序,我需要添加/删除日历范围内的所有事件。我已经成功添加和删除事件,但问题是我在日期范围内获取日历事件。我正在举办日历活动,但是当我去SPLANNER时,我可以看到这些活动不会在日历中添加,因为我已经删除了它们。我不知道从哪里得到这些events.Please建议。这是我写得到压延事件的代码: -在Android 4.0及更高版本中获取日历事件

public void onGetEvent (final String fullCallbackName, String title,String startDate,String endDate) throws JSONException 
    { 
      try 
      { 
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
       final ArrayList<JSONObject> calEvents = new ArrayList(); 
       if(calEvents.size() !=0) 
       { 
         calEvents.clear(); 
       } 

       ContentResolver contentResolver = getContentResolver(); 
       String selection = "((dtstart >= "+(dateFormat.parse(startDate).getTime())+") AND (dtend <= "+(dateFormat.parse(endDate).getTime())+"))"; 

       Cursor cursor = contentResolver.query(Uri.parse(getCalendarUriBase() + "events"), 
         (new String[] { "_id", "title", "dtstart","dtend","eventLocation","description"}), selection, null, null); 
       Log.e("cursor.getCount before:","callbackFuncName:" + cursor.getCount()); 
       while (cursor.moveToNext()) {      
        String _id = cursor.getString(0); 
        String displayName = cursor.getString(1); 
         Log.e("cursor.getCount before:","callbackFuncName:" + displayName); 


        String[] separated = displayName.split(":"); 
        if(separated[0]!= null && title.equals(separated[0])) 
        { 
         JSONObject dictionary = new JSONObject(); 

         String dstart = dateFormat.format(new Date(Long.parseLong(cursor.getString(2))));//cursor.getString(2);  
         String dEnd = dateFormat.format(new Date(Long.parseLong(cursor.getString(3))));//cursor.getString(3); 
         String eventlocation = cursor.getString(4); 
         String description = cursor.getString(5); 
         dictionary.put("identifier", _id); 
         dictionary.put("title", displayName); 
         dictionary.put("startDate", dstart); 
         dictionary.put("endDate", dEnd); 
         dictionary.put("venue", eventlocation); 
         dictionary.put("notes", description); 
         calEvents.add(dictionary); 


        } 
       } 

       if(fullCallbackName != null && !fullCallbackName.equals("")) 
       { 
         runOnUiThread(new Runnable() { 
         public void run() 
         { 

          webView.loadUrl("javascript:"+fullCallbackName+" ("+calEvents+")") ; 
         } 
        }); 
       } 


      } 
      catch(Exception e) 
      { 
      Log.e("string", e.toString()); 
      } 
     } 

    } 

代码获取压延DB是: -

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

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

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

     } 
     Log.d("Sandeep", 
       calendarUriBase); 
     // managedCursor.close(); 
     return calendarUriBase; 
    } 

回答

1

与您查询,您将看到已删除的事件,因为他们仍然在数据库(为了在下一次同步到期时能够同步删除)。这就是DELETED列的用途。

要查找开始日期和结束日期之间的所有事件,请使用CalendarContract API的Instances类,如下面的代码所示。此代码只返回可见事件!

我写了一个blog post about the CalendarContract content provider详细说明这个和其他的东西。

long begin = // starting time in milliseconds; for you probably cursor.getLong(2) 
    long end = // ending time in milliseconds; cursor.getLong(3) 
    String[] proj = 
     new String[]{ 
       Instances._ID, 
       Instances.TITLE, 
       Instances.BEGIN, 
       Instances.END, 
       Instances.EVENT_LOCATION, 
       Instances.DESCRIPTION, 
       Instances.EVENT_ID}; 
    Cursor cursor = 
     Instances.query(getContentResolver(), proj, begin, end); 
    if (cursor.getCount() > 0) { 
     // do your JSON thing 
    } 
+1

谢谢你Rittmeyer。为我工作.. :) – 2013-03-24 12:25:26

+0

太棒了。总是乐于提供帮助。 – 2013-03-24 14:15:26

相关问题