【问题标题】:Showing google calendar and adding event to it显示谷歌日历并向其添加事件
【发布时间】:2015-01-11 15:20:22
【问题描述】:

以下场景可以在安卓应用中实现吗?

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

【问题讨论】:

    标签: java android


    【解决方案1】:

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

    //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)在列表下方有一个打开事件的按钮,一旦按下:

    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,并让用户在第一次打开您的应用时选择它。再次在日历提供程序文档中进行所有操作

    【讨论】:

    • 我想要实现的是中间的东西。我想以用户可以看到其他事件的方式向用户显示日历,然后当他决定添加事件时,将定义标题、描述、地点等字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2019-11-14
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多