简体   繁体   English

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

[英]Getting calendar events in android 4.0 and above

I am new to the android development. 我是android开发的新手。 I am working on calender application where i need to add/delete and get all events in between the date range. 我正在日历应用程序上工作,我需要添加/删除并获取日期范围之间的所有事件。 I have add and delete the events successufully but problem is when i am getting the calender events in between the date range. 我已经成功添加和删除了事件,但是问题是当我在日期范围之间获取日历事件时。 I am getting the calender events but when i go to SPlanner i can see those events are not added in the calender as i have already deleted them. 我正在获取日历事件,但是当我去SPlanner时,我可以看到那些事件没有添加到日历中,因为我已经删除了它们。 I do not know from where i am getting those events.Please suggest. 我不知道从哪里得到这些事件。请提出建议。 Here is the code i have written to get the calender events:- 这是我为获取日历事件编写的代码:-

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());
            }
        }

    }  

code for getting the calender DB is:- 获取日历数据库的代码是:

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;
    }

With your query you will see deleted events because they are still in the database (for being able to sync the deletion whenever the next sync is due). 通过查询,您将看到已删除的事件,因为它们仍在数据库中(以便能够在下次同步时同步删除操作)。 That's what the DELETED column is for. 这就是DELETED列的用途。

To find all events between a start and end date use the Instances class of the CalendarContract API as in the code below. 要查找开始日期和结束日期之间的所有事件,请使用CalendarContract API的Instances类,如下面的代码所示。 This code returns only visible events! 此代码仅返回可见事件!

I've written a blog post about the CalendarContract content provider detailing this and other stuff. 我写了一篇有关CalendarContract内容提供程序博客文章,详细介绍了此内容和其他内容。

   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
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM