1

我试图让我的应用程序使用我设置的一些数据自动为日历设置一个事件。

@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);
        }
    });
}
}

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

4

1 回答 1

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 的知识。

于 2013-11-16T15:20:10.007 回答