在Android 4.0及更高版本中获取日历事件

时间:2013-03-22 18:28:29

标签: android

我是android开发的新手。我正在处理日历应用程序,我需要添加/删除并获取日期范围之间的所有事件。我已成功添加和删除事件,但问题是我在日期范围之间获取日历事件。我正在收到日历事件,但是当我去SPlanner时,我可以看到这些事件没有添加到日历中,因为我已经删除了它们。我不知道从哪里得到那些事件。请建议。以下是我为获取日历事件而编写的代码: -

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 个答案:

答案 0 :(得分: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
   }