显示谷歌日历并向其添加事件

时间:2015-01-10 21:53:46

标签: java android

以下方案是否可以在Android应用程序中实现?

  1. 向用户显示Google日历。
  2. 如果用户正在尝试添加标题,地点等事件字段,则已定义。他唯一需要做的就是点击“保存”将事件添加到日历中。

1 个答案:

答案 0 :(得分:0)

您可以使用意图启动日历事件屏幕

//all version of android
 Intent i = new Intent();

 // mimeType will popup the chooser any  for any implementing application (e.g. the built in calendar or applications such as "Business calendar"
 i.setType("vnd.android.cursor.item/event"); 

 // the time the event should start in millis. This example uses now as the start time and ends in 1 hour
 i.putExtra("beginTime", new Date().getTime()); 
 i.putExtra("endTime", new Date().getTime() + DateUtils.HOUR_IN_MILLIS);

 // the action
 i.setAction(Intent.ACTION_EDIT);
 startActivity(i);

取自:how can i open the calendar from my app?

否则,如果您想在不打开日历的情况下推送数据,可以尝试使用日历提供程序检查文档:http://developer.android.com/guide/topics/providers/calendar-provider.html(下面的编辑中的检查)

根据评论更新:

做下面的事情 -

1)使用日历提供程序查询事件:

long startMillis = 0; 
long endMillis = 0;     
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();


// Run query
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = Events.CONTENT_URI;   

String selection = "((" + Events.DTSTART + " >= ?) AND (" 
                        + Events.DTEND + " <= ?))";
String[] selectionArgs = new String[] {startMillis +"", endMillis +""}; 
// Submit the query and get a Cursor object back. 
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);

2)使用listView或gridView显示结果使用简单适配器中的光标

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
        android.R.layout.simple_list_item_1, 
        cur, 
        new String[] { Events.TITLE, [more fileds you want] }, 
        new int[] { android.R.id.text1 });

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);

3)在列表下面有一个打开事件的按钮,按下do:

long calID = 3;
long startMillis = 0; 
long endMillis = 0;     
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
...

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);

// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
// 
// ... do something with event ID
//

请注意,您需要日历ID - 您也可以查询该日历ID,并让用户在第一次打开您的应用时选择它。再次将其全部放在日历提供者文档中