在不询问用户的情况下自动在Android日历上创建活动?

时间:2013-11-16 14:04:04

标签: android google-calendar-api calendarview

我正在尝试使用我设置的一些数据自动将应用程序设置为日历。

@SuppressLint({ "NewApi", "ShowToast" })
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create a new calendar view
    CalendarView calView = (CalendarView) findViewById(R.id.calendar);

    calView.setOnDateChangeListener(new OnDateChangeListener() {
        public void onSelectedDayChange(CalendarView view, int year, int month,
                int dayOfMonth) {
            //Toast.makeText(getApplicationContext(), "" + dayOfMonth, 0).show();

            // Call built in calendar
            Calendar cal = new GregorianCalendar();
            cal.set(year, month, dayOfMonth);  // sets the date picker to the clicked date
            cal.add(Calendar.MONTH, 0); // 0 for current month
            Intent intent = new Intent(Intent.ACTION_INSERT);
            intent.setData(Events.CONTENT_URI);
            intent.putExtra(Events.TITLE, "App Event Test");
            intent.putExtra(Events.EVENT_LOCATION, "At House");
            intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, cal.getTime().getTime()); 
            intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, cal.getTime().getTime() + 3600000); //3600000 for an hour //600000 for 10 minutes
            intent.putExtra(Intent.EXTRA_EMAIL, "eden@gurango.com, elliot@gurango.com");

            Toast.makeText(getApplicationContext(), month +"-"+ dayOfMonth +"-"+ year, 0).show();

            startActivity(intent);
        }
    });
}
}

当然,这里的问题是它仍然会提示用户是否“取消”或是“完成”并在他的日历上设置此事件。有人可以给我看一个关于如何自动设置事件的链接或代码片段吗?

1 个答案:

答案 0 :(得分:2)

您需要通过CalendarContract来完成。这是一个例子,虽然我注意到你有附加字段。

{
  ContentResolver cr = getContentResolver();
  ContentValues values = new ContentValues();

  // Add to Android db; duration is null for nonrecurring events.
  values.put (Events.CALENDAR_ID, Long.toString(newCalendarId));
  values.put (Events.DTSTART, dtStart);
  values.put (Events.DTEND, dtEnd);
  values.put (Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
  values.put (Events.TITLE, title);
  Uri uri = cr.insert (Events.CONTENT_URI, values);
}

假设您有一个表单或任何收集字段数据的内容,除了一件事之外,这无需任何用户交互:您需要知道日历ID。 (Android允许用户创建任意数量的日历,可以交织或单独显示)。要获取系统已知的日历列表,请使用:

String[] projection = new String[] {
  Calendars._ID,                    // 0
  Calendars.NAME,                   // 1
  Calendars.ACCOUNT_NAME,           // 2
  Calendars.CALENDAR_DISPLAY_NAME,  // 3
  Calendars.CALENDAR_ACCESS_LEVEL,  // 4
};

Cursor calCursor = getContentResolver().query(
  Calendars.CONTENT_URI, 
  projection, 
  (Calendars.VISIBLE + " = 1 and " +
   Calendars.CALENDAR_ACCESS_LEVEL + " >= " + Calendars.CAL_ACCESS_CONTRIBUTOR),
  null, 
  Calendars._ID + " ASC");

while (calCursor.moveToNext())
{
  long id = calCursor.getLong(0);
  String name = calCursor.getString(3); // display name
  if (name == null)
    name = calCursor.getString(2); // account name
  if (name == null)
    name = "unknown";

  -- do something with id and name --
} 

警告:如果您打算创建定期活动,请注意。有关RRULE和DURATION的知识有很多。