Domanda

I am trying to make my application set an event to the Calendar automatically with some data I have set.

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

Of course the problem in here is it will still prompt the user if whether to "Cancel" or is he "Done" and set this event on his calendar. Can someone show me a link or a code snippet on how to set the event automatically?

È stato utile?

Soluzione

You need to do it through the CalendarContract. Here's a sample, though I notice you have addidtional fields.

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

Assuming you have a form or whatever to collect the field data, this works without any user interaction except for one thing: you need to know the calendar id. (Android allows the user to create an arbitrary number of calendars, which may be displayed interwoven or individually). To get a list of calendars known to the system, use:

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 --
} 

Warning: if you plan to create recurring events, watch out. There's a lot to learn about RRULE and DURATION.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top