zoukankan      html  css  js  c++  java
  • Insertion and Deletion of Calendar Events

    We can add events to our android mobiles through our android application using the Calendar Events. This is specially for 2.2 version

    Follow the below steps to do the insertion and deletion operations in Calendar events.

    Step 1:

    Firstof all we need to get the Calendar Name and Id,

    As there is a chance of having many calendar configurations in a mobile, we must find out available calendar's. Using that calendars name and id only we can proceed further

    Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"calendar_id", "displayname"}, null, null, null);
    cursor.moveToFirst();
    // Get calendars name
    String calendarNames[] = new String[cursor.getCount()];
    // Get calendars id
    calendarId = new int[cursor.getCount()];
    for (int i = 0; i < calendarNames.length; i++)
    {
    calendarId[i]
    = cursor.getInt(0);
    calendarNames[i]
    = cursor.getString(1);
    cursor.moveToNext();
    }

    Step 2:

    After getting the name and id of the calendar, we need to add the event

    ContentValues contentEvent = new ContentValues();
    // Particular Calendar in which we need to add Event
    contentEvent.put("calendar_id", calendarIds[0]);
    // Title/Caption of the Event
    contentEvent.put("title", "Wedding");
    // Description of the Event
    contentEvent.put("description", "Wedding Party");
    // Venue/Location of the Event
    contentEvent.put("eventLocation", "New York");
    // Start Date of the Event with Time
    contentEvent.put("dtstart", StartDate);
    // End Date of the Event with Time
    contentEvent.put("dtend", EndDate);
    // All Day Event
    contentEvent.put("allDay", 1);
    // Set alarm for this Event
    contentEvent.put("hasAlarm",1);
    Uri eventsUri
    = Uri.parse("content://com.android.calendar/events");
    // event is added successfully
    getContentResolver().insert(eventsUri, contentEvent);
    cursor.close();

    Step 3:

    If you like to delete the event means we can use this code

    getContentResolver().delete(Uri.parse("content://com.android.calendar/events"), "calendar_id=? and description=? and eventLocation=? ", new String[]{String.valueOf(calendarIds[0]), "Wedding Party", "New York"});

    The same event can be insert or delete in 2.1, for that we can use like this

    Step 1:

    For this we can use the same code as mentioned in the Step 1 of 2.2 version.

    Step 2:

    We need to change the path as "content://calendar/calendars" from "content://com.android.calendar/calendars"

    Step 3:

    Don't take the above 2.2 version code, we need to use like this

    Uri CALENDAR_URI = Uri.parse("content://calendar/events");
    Cursor cursors
    = getContentResolver().query(CALENDAR_URI, null, null, null, null);
    if (cursors.moveToFirst())
    {
    while (cursors.moveToNext())
    {
    String desc
    = cursors.getString(cursors.getColumnIndex("description"));
    String location
    = cursors.getString(cursors.getColumnIndex("eventLocation"));
    // event id
    String id = cursors.getString(cursors.getColumnIndex("_id"));
    if ((desc==null) && (location == null))
    {
    }
    else
    {
    if (desc.equals("Birthday Party") && location.equals("Delhi"))
    {
    Uri uri
    = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(id));
    getContentResolver().delete(uri,
    null, null);
    }
    }
    }
    }

  • 相关阅读:
    Extjs4.0以上版本 Ext.Ajax.request请求的返回问题
    C# NPOI 操作Excel 案例
    C# Microsoft.Office 操作Excel总结
    asp.net core NLog将日志写到文件
    新装的SSMS一打开就显示VS许可证过期,但VS又运行正常,解决方法。
    sql server 查询log日志 sql语句
    sql server 删除所有表及所有存储过程、所有视图和递归查询、数字类型转为字符串
    C#使用Selenium+PhantomJS抓取数据
    python爬虫实例项目大全
    SQL Server TVPs 批量插入数据
  • 原文地址:https://www.cnblogs.com/enricozhang/p/1998508.html
Copyright © 2011-2022 走看看