我已成功将事件添加到本机日历,并且适用于 Android API 级别 14 或更高版本。
我尝试了@VishalKhakhkhar 的代码,但我仍然无法解决这个问题?我收到错误:在运行 2.3.6 的设备上进行测试时,找不到处理 Intent { act=android.intent.action.EDIT typ=vnd.android.cursor.item/event的活动
请参阅下面的更新代码,尝试解决此问题:-
Button btnEvents = (Button) findViewById(R.id.button1);
btnEvents.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2014, 1, 16, 16, 0);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2014, 1, 16, 22, 30);
endMillis= endTime.getTimeInMillis();
if (Build.VERSION.SDK_INT >= 14) {
Intent intent = new Intent(Intent.ACTION_EDIT).setData(Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startMillis)
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endMillis)
.putExtra(Events.TITLE, "Title")
.putExtra(Events.DESCRIPTION, "Description")
.putExtra(Events.EVENT_LOCATION, "Location")
.putExtra(Reminders.HAS_ALARM, true)
.putExtra(Reminders.METHOD, Reminders.METHOD_DEFAULT);
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Title");
intent.putExtra("description", "Description");
intent.putExtra("beginTime", startMillis);
intent.putExtra("allDay", false);
intent.putExtra("endTime", endTime.endMillis);
intent.putExtra("eventLocation", "Location");
intent.putExtra("hasAlarm", 1);
startActivity(intent);
}
}
});
}