注意!!!
多个重复的事件只会有一条数据,例如创建了一个每天都提醒的事件,获取事件时只会有第一天的数据,其它的时间不会有;
首先获取日历权限
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
static String[] permissions = new String[] { "android.permission.READ_CALENDAR"};
ActivityCompat.requestPermissions(activity, permissions, 0);
然后获取日程事件:
获取到的数据如下图
[{"endTime":"2022-06-08 20:55:00","eventTitle":"我是标题","location":"上海","startTime":"2022-06-08 19:15:00","week":"3"},{"description":"说明一下情况啊","endTime":"2022-06-03 20:00:00","eventTitle":"端午节啦啦啦","location":"上海","startTime":"2022-06-03 19:00:00","week":"5"}]
public static ArrayList<CalenderDataStruct> GetCalenderSchedule(Context context){
String startTime = "";
String endTime = "";
String eventTitle = "";
String description = "";
String location = "";
String week = "";
ArrayList<CalenderDataStruct> arr=new ArrayList<CalenderDataStruct>();
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null,
null, null, "dtstart"+" DESC");
while (eventCursor.moveToNext()){
eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
description = eventCursor.getString(eventCursor.getColumnIndex("description"));
location = eventCursor.getString(eventCursor.getColumnIndex("eventLocation"));
startTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtstart"))));
endTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtend"))));
week = ""+ (getWeek(startTime));
CalenderDataStruct item=new CalenderDataStruct(eventTitle, startTime, endTime,description, location,week);
arr.add(item);
}
return arr;
}
全部代码, 获取全部日程事件、获取几天内的日程事件都在下面;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CalendarUtil {
private static final String TAG ="Main_calendarUtil";
private static String CALENDER_URL = "content://com.android.calendar/calendars";
private static String CALENDER_EVENT_URL = "content://com.android.calendar/events";
private static String CALENDER_REMINDER_URL = "content://com.android.calendar/reminders";
private static TelephonyManager mTm;
// 当天当前的时间变量
private static long currentTime;
// 当天之后的几天后的时间变量
private static long afterDayTime = Long.MAX_VALUE;
// 一天总共的毫秒数
private static long onedayMS = 1000*60*60*24;
/**
* 添加从当天开始获取之后几天的日历日程事件
* @param context
* @param afterDays
* @return
*/
public static ArrayList<CalenderDataStruct> GetCalenderScheduleFormNow(Context context,int afterDays){
currentTime = System.currentTimeMillis();
if (afterDays > 0){
afterDayTime = afterDays * onedayMS +currentTime;
}
String startTime = "";
String endTime = "";
String eventTitle = "";
String description = "";
String location = "";
String week = "";
ArrayList<CalenderDataStruct> arr=new ArrayList<CalenderDataStruct>();
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null,
null, null, "dtstart"+" DESC");
while (eventCursor.moveToNext()){
long dtstart = Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtstart")));
// 满足获取条件的日历日程数据
if( currentTime < dtstart && dtstart < afterDayTime){
eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
description = eventCursor.getString(eventCursor.getColumnIndex("description"));
location = eventCursor.getString(eventCursor.getColumnIndex("eventLocation"));
startTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtstart"))));
endTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtend"))));
week = ""+ (getWeek(startTime));
CalenderDataStruct item=new CalenderDataStruct(eventTitle, startTime, endTime,description, location,week);
arr.add(item);
}
}
return arr;
}
public static ArrayList<CalenderDataStruct> GetCalenderSchedule(Context context){
String startTime = "";
String endTime = "";
String eventTitle = "";
String description = "";
String location = "";
String week = "";
ArrayList<CalenderDataStruct> arr=new ArrayList<CalenderDataStruct>();
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null,
null, null, "dtstart"+" DESC");
while (eventCursor.moveToNext()){
eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
description = eventCursor.getString(eventCursor.getColumnIndex("description"));
location = eventCursor.getString(eventCursor.getColumnIndex("eventLocation"));
startTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtstart"))));
endTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtend"))));
week = ""+ (getWeek(startTime));
CalenderDataStruct item=new CalenderDataStruct(eventTitle, startTime, endTime,description, location,week);
arr.add(item);
}
return arr;
}
private static JSONArray getCalendar(Context context){
String startTime = "";
String endTime = "";
String eventTitle = "";
String description = "";
String location = "";
JSONArray arr=new JSONArray();
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null,
null, null, null);
while (eventCursor.moveToNext()){
JSONObject json=new JSONObject();
eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
description = eventCursor.getString(eventCursor.getColumnIndex("description"));
location = eventCursor.getString(eventCursor.getColumnIndex("eventLocation"));
startTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtstart"))));
endTime = timeStamp2Date(Long.parseLong(eventCursor.getString(eventCursor.getColumnIndex("dtend"))));
try {
json.put("eventTitle",eventTitle);
json.put("description",description);
json.put("location",location);
json.put("startTime",startTime);
json.put("endTime",endTime);
} catch (JSONException e) {
e.printStackTrace();
}
arr.put(json);
}
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));
}
/**
* 判断当前日期是星期几
*
* @param pTime 设置的需要判断的时间 //格式如2012-09-08
*
* @return dayForWeek 判断结果
* @Exception 发生异常
*/
// String pTime = "2012-03-12";
private static int getWeek(String pTime) {
int Week = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(pTime));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
Week = 0;
}
if (c.get(Calendar.DAY_OF_WEEK) == 2) {
Week = 1;
}
if (c.get(Calendar.DAY_OF_WEEK) == 3) {
Week = 2;
}
if (c.get(Calendar.DAY_OF_WEEK) == 4) {
Week = 3;
}
if (c.get(Calendar.DAY_OF_WEEK) == 5) {
Week = 4;
}
if (c.get(Calendar.DAY_OF_WEEK) == 6) {
Week = 5;
}
if (c.get(Calendar.DAY_OF_WEEK) == 7) {
Week = 6;
}
return Week;
}
// 添加需要的动态申请的权限(危险权限才会弹出跳出)
static String[] permissions = new String[] { "android.permission.READ_CALENDAR"};
/**
* 检测权限,然后动态申请未授权的权限
* (给 Unity 使用)
* @param permissions Unity 传给来的权限字符串
*/
public static void checkPermissionsForUnity( Activity activity){
// 申请权限
checkPermissions(activity, permissions);
}
/**
* 动态权限申请,可给Android 自己调用
* @param activity
* @param MANDATORY_PERMISSIONS
*/
private static void checkPermissions(Activity activity, String[] MANDATORY_PERMISSIONS) {
Log.i(TAG, "checkPermissions: 检测权限");
// 判断授权列表
List<String> unGrantedPermissions;
unGrantedPermissions = new ArrayList();
// 检测权限
for (String permission : MANDATORY_PERMISSIONS) {
Log.i(TAG, "checkPermissions: permission " +permission);
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
unGrantedPermissions.add(permission);
}
}
// 权限请求是否授权
if (unGrantedPermissions.size() != 0) {
String[] array = new String[unGrantedPermissions.size()];
ActivityCompat.requestPermissions(activity, unGrantedPermissions.toArray(array), 0);
Log.i(TAG, "checkPermissions: 已经获得了所有权限");
}
Log.i(TAG, "checkPermissions: Is Over");
}
}
JavaBean:
public class CalenderDataStruct{
public CalenderDataStruct(String eventTitle, String startTime, String endTime, String description, String location, String week) {
this.eventTitle = eventTitle;
this.startTime = startTime;
this.endTime = endTime;
this.description = description;
this.location = location;
this.week = week;
}
public String eventTitle = "";
public String startTime;
public String endTime;
public String description;
public String location;
public String week;
}
如何使用:
findViewById(R.id.richeng).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, "android.permission.READ_CALENDAR") != PackageManager.PERMISSION_GRANTED) {
CalendarUtil.checkPermissionsForUnity(MainActivity.this);
}else{
ArrayList<CalenderDataStruct> s = CalendarUtil.GetCalenderScheduleFormNow(MainActivity.this,15);
Log.i("CalenderDataStruct", new Gson().toJson(s));
}
}
});
注意!!!
多个重复的事件只会有一条数据,例如创建了一个每天都提醒的事件,获取事件时只会有第一天的数据,其它的时间不会有;
Cursor eventCursor = CalendarContract.Instances.query(context.getContentResolver() , null , currentTime , afterDayTime);