- 首先,确保你有正确的权限,添加如下代码到AndroidManifest.xml文件中:
- 在Java文件中,创建一个新的Intent对象来操作Calendar对象,例如:
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent);
- 如果你需要监听日历事件,可以使用Activity.startActivityForResult或ActivityResultLauncher.registerForActivityResult方法来监听结果,例如:
ActivityResultLauncher activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
// Handle calendar event insertion success
} else {
// Handle calendar event insertion failure
}
}
);
activityResultLauncher.launch(intent);