Android手机配备有一个内置的日历应用程序

http://www.oschina.net/code/explore/android-2.2-froyo/com/android/calendar/EventInfoActivity.java
http://tech.it168.com/a2010/0209/850/000000850667.shtml
http://www.cnblogs.com/enricozhang/archive/2011/03/29/1998508.html
http://407827531.iteye.com/blog/891522
http://wang-peng1.iteye.com/blog/1042693

http://mypyg.iteye.com/category/115901
Android手机配备有一个内置的日历应用程序。第三方应用程序可以利用日历内容提供商接口读取用户的日历信息和安排在日历新的事件。这个日历可以直接同步用户的谷歌日历。

  不幸的是,没有文档和Android手机的日历应用集成,因为有另外一个联系人应用程序。相反,本文所提供的所有信息,将会通过逆向工程的谷歌日历内容提供商。该接口是受变化的,将会支持有限的功能。然而,日历一体化可以成为一些类型的应用强大的功能。

  本文的代码测试之到Android 2.0 SDK版本。我们将发布一个更新如果有一个很大的转变。我们没有测试这个装置,如早先的T - Mobile G1的(SDK的1.6)代码。

  访问日历数据

  为了支持添加到您的日历的Android应用程序,您必须给你的应用程序添加以下权限AndroidManifest.xml文件:
1   android:name="android.permission.READ_CALENDAR">
2
3   android:name="android.permission.WRITE_CALENDAR">
4

  注册这些权限允许您访问谷歌登录用户的日历数据,无需处理任何问题。该日历应用程序未安装在Android模拟器,因此所有的测试和开发必须在日历应用程序实际完成安装的设备(使用模拟器将无法启动相应的提供者)。日历应用程序和内容提供商可在与谷歌服务,例如T - Mobile G1。

  检索用户日历列表

  用户可能在日历应用程序配置有很多名称的日历。例如,用户可能有一个工作日历(工作日历有关的活动),家庭日历(个人的)和假日日历(法定假日)。

  由用户配置的日历使用并访问内容提供商接口。为了检索用户的日历列表,我们需要为适当Uri以及日历内容提供商设计查询,如下所示:
1   String[] projection = new String[] { "_id", "name" };
2
3   Uri calendars = Uri.parse("content://calendar/calendars");
4
5   Cursor managedCursor =
6
7   managedQuery(calendars, projection, null, null, null);
8

  现在,这个查询将返回所有日历,包括那些平时不会用到的。为了得到一个活跃的日历列表单,我们需要在“选择”设置为true的领域在内限制我们的查询
1   String[] projection = new String[] { "_id", "name" };
2
3   Uri calendars = Uri.parse("content://calendar/calendars");
4
5   Cursor managedCursor =
6
7   managedQuery(calendars, projection,
8
9   "selected=1", null, null);
10

  我们现在检索的日历列表。我们可以遍历的结果如下:
1 if (managedCursor.moveToFirst()) {
2
3   String calName;
4
5   String calId;
6
7   int nameColumn = managedCursor.getColumnIndex("name");
8
9   int idColumn = managedCursor.getColumnIndex("_id");
10
11   do {
12
13   calName = managedCursor.getString(nameColumn);
14
15   calId = managedCursor.getString(idColumn);
16
17   } while (managedCursor.moveToNext());
18
19   }
20
21

  一旦我们知道我们想要访问的日历,我们可以添加一个日历事件。日历事件有一些重要领域,其中包括如活动名称,时间和地点以及设置的信息,哪一项将被显示在日历。日历事件可能是一次性或经常性的。

  给日历增加单一事件发生的情况

  添加一个条目到特定的日历,我们需要配置一个日历项插入使用与ContentValues如下:
1   ContentValues event = new ContentValues();

  每个活动必须与特定日历结合,所以,首先你会想到的是为此事件插入日历标识符。
1   event.put("calendar_id", calId);

  然后,我们设置了有关事件,其中包括活动的标题,描述和位置弦乐领域的一些基本信息。
1   event.put("title", "Event Title");
2
3   event.put("description", "Event Desc");
4
5   event.put("eventLocation", "Event Location");
6

  有许多不同的配置选项来设置事件的时间和日期。

  我们可以设置事件的开始和结束的信息如下:
1   long startTime = START_TIME_MS;
2
3   long endTime = END_TIME_MS;
4
5   event.put("dtstart", startTime);
6
7   event.put("dtend", endTime);
8

  如果我们增加了生日或假日,我们会设置一个全天事件的条目:
1   event.put("allDay", 1); // 0 for false, 1 for true

  这一信息对于大多数项足够了。但是,有一些其他有用的日历项属性。

  例如,您可以设置事件状态暂定(0),确认(1)或取消(2):
1   event.put("eventStatus", 1);

  您可以控制可以看到它的可见性设置为默认值(0此事件),保密(1),私营(2),或公共(3):
1   event.put("visibility", 0);

  您可以控制日历上事件是否消耗时间,通过设置其透明度,不透明(0)或透明(1)。
1   event.put("transparency", 0);

  您可以控制是否事件触发报警,提醒如下:
1 event.put("hasAlarm", 1); // 0 for false, 1 for true

  一旦日历事件配置正确,我们已经准备好使用ContentResolver插入到相应的开放新日历的日历事件项:
1   Uri eventsUri = Uri.parse("content://calendar/events");
2
3   Uri url = getContentResolver().insert(eventsUri, event);
4

  该调用insert()方法接触的日历内容提供商,并试图插入到相应的用户的日历项。如果您导航到日历应用程序和启动它,你应该看到您在适当的日历中的日历项。自日历同步,你也看到日历项在线,如果你在网络上使用的谷歌日历。

  添加一个定期事件的日历

  您也可以设定定期日历事件。为了做到这一点,你必须根据当前的规则添加更多的字段。规则是根据RFC2445。

  结论

  Android应用程序可以集成与用户的日历密切的许多Android设备。该日历功能是通过一条内容提供商的接口,允许第三方应用程序访问日历信息,并添加新日历项。

访问用户事件也是用这个:Uri eventsUri = Uri.parse("content://calendar/events");而Uri calendars = Uri.parse("content://calendar/calendars");只是访问用户是用哪个帐号登录的android日历,而且android日历的源代码也是这样说的


String[] projection = new String[] { "_id", "title" };
        Uri calendars = Uri.parse("content://calendar/events");
       
        Cursor managedCursor = managedQuery(calendars, projection, "selected=1", null, null);
       
        if (managedCursor.moveToFirst()) {
            String calName;
            String calId;
            int nameColumn = managedCursor.getColumnIndex("title");
            int idColumn = managedCursor.getColumnIndex("_id");
            do{
                calName = managedCursor.getString(nameColumn);
                Log.i(tag, "calName = " + calName);
                calId = managedCursor.getString(idColumn);
                Log.i(tag, "calId = " + calId);
//                ContentValues event = new ContentValues();
//                event.put("calendar_id", calId);
//                event.put("title", "Event Title");
//                event.put("description", "Event Desc");
//                event.put("eventLocation", "Event Location");
//                long startTime = new Date(2010,11,11,10,10,10).getTime();
//                long endTime = new Date(2010,11,11,10,11,10).getTime();
//                event.put("dtstart", startTime);
//                event.put("dtend", endTime);
//                Uri eventsUri = Uri.parse("content://calendar/events");
//                getContentResolver().insert(eventsUri, event);
              
              
            }while(managedCursor.moveToNext());




Intent intent = new Intent(Intent.ACTION_VIEW);
//Android 2.2+
intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID))); 
//Android 2.1 and below.
//intent.setData(Uri.parse("content://calendar/events/" + String.valueOf(calendarEventID)));   
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_SINGLE_TOP
        | Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_NO_HISTORY
        | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);

**
 * Add a calendar event.
 */
private void addCalendarEvent(){
    Context context = getContext();
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_NO_HISTORY
            | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    context.startActivity(intent);
}

/**
 * Edit a calendar event.
 */
private void editCalendarEvent(){
    Context context = getContext();
    long calendarEventID = .....
    intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID)));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_SINGLE_TOP
        | Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_NO_HISTORY
        | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
   context.startActivity(intent);
}



android是带有日历程序的,只不过没有装入apk而已,你可以通过adb shell或是File Explorer下的data/data下有个com.android.calendar 可见系统自带有日历程序的。
  关于如何安装这个日历程序,我曾花了大半天的时间尝试,总算是成功装入,首先,谷歌的这个calendar 是需要专有calendar provider支持的,也就是说即使你把calendar.apk装入系统也是异常,无法正常启动的,这时候,你还需要将 com.android.provider.calendar编译成apk文件install进系统,此是该日历就能重见天日了。
这是我在网上看到的,没测试过,你可以试试
            String date=tmr.getTime();
               
                String format = "yyyy-MM-dd HH:mm:ss";
                SimpleDateFormat sf = new SimpleDateFormat(format);
                long temp=sf.parse(date).getTime();


Android,I need the code that delete all the event of my calendar in my device

private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) {
    Cursor cursor;
    if (android.os.Build.VERSION.SDK_INT <= 7) { //up-to Android 2.1
        cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars._id=" + calendarId, null, null);
    } else { //8 is Android 2.2 (Froyo) (http://developer.android.com/reference/android/os/Build.VERSION_CODES.html)
        cursor = resolver.query(eventsUri, new String[]{ "_id" }, "calendar_id=" + calendarId, null, null);
    }
    while(cursor.moveToNext()) {
        long eventId = cursor.getLong(cursor.getColumnIndex("test"));
        resolver.delete(ContentUris.withAppendedId(eventsUri, eventId), null, null);
    }
    cursor.close();
}

And that is the code that calling the delete function:

    Uri eventsUri;
    int calendarId=1;
    int osVersion = android.os.Build.VERSION.SDK_INT;
    if (osVersion <= 7) { //up-to Android 2.1
        eventsUri = Uri.parse("content://calendar/events");
    } else { //8 is Android 2.2 (Froyo) (http://developer.android.com/reference/android/os/Build.VERSION_CODES.html)
        eventsUri = Uri.parse("content://com.android.calendar/events");
    }
    ContentResolver resolver = this.getContentResolver();
    deleteEvent(resolver, eventsUri, calendarId);


//read calendar database
Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),    null, null, null, null);
cursor.moveToFirst();

// fetching calendars id
if(cursor.getcount>0)
{
CId = new int[cursor.getCount()];
int i=0;
while(!cursor.isAfterLast())
{
     CId[i] = cursor.getInt(cursor.getColumnIndex("_id"));
     i++;
     cursor.moveToNext();
}

   

read calendar database

Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),    null, null, null, null);
cursor.moveToFirst();

// fetching calendars id
if(cursor.getcount>0)
{
CId = new int[cursor.getCount()];
int i=0;
while(!cursor.isAfterLast())
{
     CId[i] = cursor.getInt(cursor.getColumnIndex("_id"));
     i++;
     cursor.moveToNext();
}

//delete a calender event
for (int i = 0; i < CNames.length; i++)
{
Uri CALENDAR_URI = Uri.parse("content://calendar/events");
Uri uri = ContentUris.withAppendedId(CALENDAR_URI,Cid[i]);
getContentResolver().delete(uri, null, null);
}
}                      

String[] projection = new String[] { "_id", "title" };
        Uri calendars = Uri.parse("content://calendar/events");
       
        Cursor managedCursor = managedQuery(calendars, projection, "selected=1", null, null);
       
        if (managedCursor.moveToFirst()) {
            String calName;
            String calId;
            int nameColumn = managedCursor.getColumnIndex("title");
            int idColumn = managedCursor.getColumnIndex("_id");
            do{
                calName = managedCursor.getString(nameColumn);
                Log.i(tag, "calName = " + calName);
                calId = managedCursor.getString(idColumn);
                Log.i(tag, "calId = " + calId);
//                ContentValues event = new ContentValues();
//                event.put("calendar_id", calId);
//                event.put("title", "Event Title");
//                event.put("description", "Event Desc");
//                event.put("eventLocation", "Event Location");
//                long startTime = new Date(2010,11,11,10,10,10).getTime();
//                long endTime = new Date(2010,11,11,10,11,10).getTime();
//                event.put("dtstart", startTime);
//                event.put("dtend", endTime);
//                Uri eventsUri = Uri.parse("content://calendar/events");
//                getContentResolver().insert(eventsUri, event);
              
              
            }while(managedCursor.moveToNext());




Intent intent = new Intent(Intent.ACTION_VIEW);
//Android 2.2+
intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID))); 
//Android 2.1 and below.
//intent.setData(Uri.parse("content://calendar/events/" + String.valueOf(calendarEventID)));   
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_SINGLE_TOP
        | Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_NO_HISTORY
        | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);

**
 * Add a calendar event.
 */
private void addCalendarEvent(){
    Context context = getContext();
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_NO_HISTORY
            | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    context.startActivity(intent);
}

/**
 * Edit a calendar event.
 */
private void editCalendarEvent(){
    Context context = getContext();
    long calendarEventID = .....
    intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID)));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_SINGLE_TOP
        | Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_NO_HISTORY
        | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
   context.startActivity(intent);
}


/**
 * Adds the event to a calendar. It lets the user choose the calendar
 * @param ctx Context ( Please use the application context )
 * @param title title of the event
 * @param dtstart Start time: The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
 * @param dtend End time: The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
 */
private static void addToCalendar(Context ctx, final String title, final long dtstart, final long dtend) {
    final ContentResolver cr = ctx.getContentResolver();
    Cursor cursor ;
    if (Integer.parseInt(Build.VERSION.SDK) == 8 )
        cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    else
        cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    if ( cursor.moveToFirst() ) {
        final String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        for (int i = 0; i < calNames.length; i++) {
            calIds[i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            cursor.moveToNext();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog,    int which) {
                ContentValues cv = new ContentValues();
                cv.put("calendar_id", calIds[which]);
                cv.put("title", title);
                cv.put("dtstart", dtstart );
                cv.put("hasAlarm", 1);
                cv.put("dtend", dtend);

                Uri newEvent ;
                if (Integer.parseInt(Build.VERSION.SDK) == 8 )
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
                else
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);

                if (newEvent != null) {
                    long id = Long.parseLong( newEvent.getLastPathSegment() );
                    ContentValues values = new ContentValues();
                    values.put( "event_id", id );
                    values.put( "method", 1 );
                    values.put( "minutes", 15 ); // 15 minuti
                    if (Integer.parseInt(Build.VERSION.SDK) == 8 )
                        cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                    else
                        cr.insert( Uri.parse( "content://calendar/reminders" ), values );

                }
                dialog.cancel();
            }

        });

        builder.create().show();
    }
    cursor.close();
}

Hope it helps to solve the problem about google calendar in froyo :)

posted @ 2011-09-24 21:29  jacky.YL.G  阅读(2412)  评论(0)    收藏  举报