首先获取日历权限
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
String[] permissions = new String[] { "android.permission.READ_CALENDAR"};
ActivityCompat.requestPermissions(activity, permissions, 0);
获取最近几天的日历日程,下面的是最近15天;
这个方法会获取指定时间段里每天的日程事件;
Cursor eventCursor = CalendarContract.Instances.query(context.getContentResolver() , null , currentTime , afterDayTime);
// 一天总共的毫秒数
private long onedayMS = 1000*60*60*24;
* 添加从当天开始获取之后几天的日历日程
* @param context
* @return
public static ArrayList<CalenderDataStruct> GetCalender15Day(Context context){
String startTime = "";
String endTime = "";
String eventTitle = "";
String description = "";
String location = "";
String currentTime = System.currentTimeMillis();
String afterDayTime = 15 * onedayMS +currentTime;
Cursor eventCursor = CalendarContract.Instances.query(context.getContentResolver() , null , currentTime , afterDayTime);
ArrayList<CalenderDataStruct> arr=new ArrayList();
while (eventCursor != null && eventCursor.moveToNext()){
eventTitle = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.TITLE));
description = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.DESCRIPTION));
location = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.EVENT_LOCATION));
startTime =eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.BEGIN));
endTime = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.END));
CalenderDataStruct item=new CalenderDataStruct(eventTitle, timeStamp2Date(Long.parseLong(startTime)), timeStamp2Date(Long.parseLong(endTime)),description, location,"");
arr.add(item);
return arr;
* 时间戳转换为字符串
* @param time:时间戳
* @return
private static String timeStamp2Date(long time) {
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(time));
CalendarContract.Instances.TITLE //事件标题
CalendarContract.Instances.DESCRIPTION //事件详情
CalendarContract.Instances.EVENT_LOCATION 事件位置信息
CalendarContract.Instances.DTSTART //事件开始时间
CalendarContract.Instances.DTEND //事件结束时间
CalendarContract.Instances.BEGIN //事件提醒开始时间 - (The beginning time of the instance, in UTC milliseconds.)
CalendarContract.Instances.END //事件提醒开始时间 - (The ending time of the instance, in UTC milliseconds.)
如果需要获取提醒时间(10分钟前提醒、1小时前提醒),则需要用日程事件的ID去查询;使用这个 CalendarContract.Reminders.query 方法查询;
String[] projection = {CalendarContract.Reminders.MINUTES , CalendarContract.Reminders.METHOD};
Cursor remindersCursor = CalendarContract.Reminders.query(context.getContentResolver() , eId , projection);
String minutes = "";
String method = "";
while (remindersCursor != null && remindersCursor.moveToNext()){
minutes += remindersCursor.getString(remindersCursor.getColumnIndex(CalendarContract.Reminders.MINUTES)) + " , ";
method += remindersCursor.getString(remindersCursor.getColumnIndex(CalendarContract.Reminders.METHOD)) + " , ";
全部的--:
* 添加从当天开始获取之后几天的日历日程
* @param context
* @return
@SuppressLint("Range")
public static ArrayList<CalenderDataStruct> GetCalender14(Context context){
String startTime = "";
String endTime = "";
String eventTitle = "";
String description = "";
String location = "";
currentTime = System.currentTimeMillis();
afterDayTime = 14 * onedayMS +currentTime;
Cursor eventCursor = CalendarContract.Instances.query(context.getContentResolver() , null , currentTime , afterDayTime);
ArrayList<CalenderDataStruct> arr=new ArrayList();
while (eventCursor != null && eventCursor.moveToNext()){
eventTitle = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.TITLE));
description = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.DESCRIPTION));
location = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.EVENT_LOCATION));
startTime =eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.BEGIN));
endTime = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Instances.END));
long eId = eventCursor.getLong(eventCursor.getColumnIndex(CalendarContract.Instances.EVENT_ID));
String[] projection = {CalendarContract.Reminders.MINUTES , CalendarContract.Reminders.METHOD};
Cursor remindersCursor = CalendarContract.Reminders.query(context.getContentResolver() , eId , projection);
String minutes = "";
String method = "";
while (remindersCursor != null && remindersCursor.moveToNext()){
minutes += remindersCursor.getString(remindersCursor.getColumnIndex(CalendarContract.Reminders.MINUTES)) + " , ";
method += remindersCursor.getString(remindersCursor.getColumnIndex(CalendarContract.Reminders.METHOD)) + " , ";
CalenderDataStruct item = new CalenderDataStruct(eventTitle, timeStamp2Date(Long.parseLong(startTime)), timeStamp2Date(Long.parseLong(endTime)),description, location,minutes);
arr.add(item);
for (int i = 0; i < eventCursor.getColumnNames().length; i++) {
Log.i(TAG, "GetCalender---: " +eventCursor.getColumnNames()[i] );
Log.i(TAG, "GetCalender: " +arr.size() );
Log.i(TAG, "GetCalender Json: " +(new Gson().toJson(arr)));
return arr;
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
2、动态权限Activity中
if (ContextCompat.checkSelfPermission(this,
Manifest.perm...
Android Studio是一款非常流行的Android应用开发工具,可以用来开发各种类型的应用程序,包括日历应用。在Android Studio中,可以使用Java或Kotlin编写代码,使用XML文件设计用户界面,并使用内置的工具和库来创建功能强大的日历应用程序。开发日历应用程序需要掌握一些基本的Android开发知识,例如布局、视图、活动、片段和存储等。同时,还需要了解如何使用Android SDK中的日历API来访问设备的日历数据,并将其显示在应用程序中。总之,使用Android Studio开发日历应用程序需要一定的技术水平和经验,但是一旦掌握了相关技能,就可以轻松地创建出高质量的日历应用程序。
unable to access ‘https://github.com/***/‘: HTTP/2 stream 1 was not closed cleanly before end
jouurney:
己用Okhttp工具类
jouurney: