1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| package com.cocos.calender;
import android.app.Activity; import android.Manifest; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.provider.CalendarContract; import android.provider.Settings; import android.text.TextUtils; import android.util.Log;
import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat;
import java.util.Calendar; import java.util.TimeZone; import org.json.JSONObject; import org.json.JSONException; import android.widget.Toast;
public class CalendarHelper {
private static final String TAG = "CalendarHelper"; private static String denyPermissionMessage = "未获得日历权限,无法添加提醒事件"; public static final int REQUEST_CALENDAR_PERMISSION = 1010;
private static PendingEvent pendingEvent;
private static class PendingEvent { String title; String description; String location; long beginTime; long endTime; int reminderMinutes;
PendingEvent(String title, String description, String location, long beginTime, long endTime, int reminderMinutes) { this.title = title; this.description = description; this.location = location; this.beginTime = beginTime; this.endTime = endTime; this.reminderMinutes = reminderMinutes; } }
public static void addEventWithPermission(Activity activity, String title, String description, String location, long beginTimeMillis, long endTimeMillis, int reminderMinutes) {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
pendingEvent = new PendingEvent(title, description, location, beginTimeMillis, endTimeMillis, reminderMinutes);
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_CALENDAR)) { Log.i(TAG, "需要日历权限来添加提醒事件"); }
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR}, REQUEST_CALENDAR_PERMISSION);
} else { addEvent(activity, title, description, location, beginTimeMillis, endTimeMillis, reminderMinutes); } }
public static void onRequestPermissionsResultCalendar(Activity activity, int requestCode, @NonNull int[] grantResults) { if (requestCode == REQUEST_CALENDAR_PERMISSION) { if (grantResults.length >= 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "日历权限申请成功");
if (pendingEvent != null) { addEvent(activity, pendingEvent.title, pendingEvent.description, pendingEvent.location, pendingEvent.beginTime, pendingEvent.endTime, pendingEvent.reminderMinutes); pendingEvent = null; }
} else { Log.e(TAG, "用户拒绝了日历权限"); Toast.makeText(activity, denyPermissionMessage, Toast.LENGTH_SHORT).show();
} } }
private static long getCalendarAccountId(Context context) { Cursor userCursor = context.getContentResolver().query( CalendarContract.Calendars.CONTENT_URI, new String[]{CalendarContract.Calendars._ID}, null, null, null);
if (userCursor != null) { try { if (userCursor.moveToFirst()) { return userCursor.getLong(0); } } finally { userCursor.close(); } } return -1; }
private static boolean isEventAlreadyExists(Context context, String title, long beginTimeMillis) { long oneMinuteBefore = beginTimeMillis - 60 * 1000; long oneMinuteAfter = beginTimeMillis + 60 * 1000;
Cursor cursor = context.getContentResolver().query( CalendarContract.Events.CONTENT_URI, new String[]{CalendarContract.Events._ID}, CalendarContract.Events.TITLE + "=? AND " + CalendarContract.Events.DTSTART + ">=? AND " + CalendarContract.Events.DTSTART + "<=?", new String[]{title, String.valueOf(oneMinuteBefore), String.valueOf(oneMinuteAfter)}, null );
if (cursor != null) { try { if (cursor.moveToFirst()) { return true; } } finally { cursor.close(); } } return false; }
private static boolean addEvent(Context context, String title, String description, String location, long beginTimeMillis, long endTimeMillis, int reminderMinutes) { long calId = getCalendarAccountId(context); if (calId == -1) { Log.e(TAG, "没有找到系统日历账户,请先在系统日历中添加一个账户"); return false; }
if (isEventAlreadyExists(context, title, beginTimeMillis)) { Log.w(TAG, "事件已存在,跳过添加: " + title); return false; }
ContentValues eventValues = new ContentValues(); eventValues.put(CalendarContract.Events.CALENDAR_ID, calId); eventValues.put(CalendarContract.Events.TITLE, TextUtils.isEmpty(title) ? "未命名事件" : title); eventValues.put(CalendarContract.Events.DESCRIPTION, description); eventValues.put(CalendarContract.Events.EVENT_LOCATION, location); eventValues.put(CalendarContract.Events.DTSTART, beginTimeMillis); eventValues.put(CalendarContract.Events.DTEND, endTimeMillis); eventValues.put(CalendarContract.Events.HAS_ALARM, 1); eventValues.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri newEvent = context.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, eventValues); if (newEvent == null) { Log.e(TAG, "插入日历事件失败"); return false; }
long eventId = ContentUris.parseId(newEvent);
ContentValues reminderValues = new ContentValues(); reminderValues.put(CalendarContract.Reminders.EVENT_ID, eventId); reminderValues.put(CalendarContract.Reminders.MINUTES, reminderMinutes); reminderValues.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri reminderUri = context.getContentResolver().insert(CalendarContract.Reminders.CONTENT_URI, reminderValues); if (reminderUri == null) { Log.e(TAG, "插入提醒失败"); return false; }
Log.i(TAG, "日历事件添加成功,eventId=" + eventId); return true; } public static void creatroCalendarReminder(Context context,String data){ try { JSONObject json = new JSONObject(data);
String title = json.optString("title", "测试"); String description = json.optString("description", "测试"); String location = json.optString("location", "测试"); int startHour = json.optInt("startHour", 1); int startMinute = json.optInt("startMinute", 10); int endHour = json.optInt("endHour", startHour + 1);
Calendar begin = Calendar.getInstance(); begin.add(Calendar.DAY_OF_MONTH, 0); begin.set(Calendar.HOUR_OF_DAY, startHour); begin.set(Calendar.MINUTE, startMinute);
Calendar end = (Calendar) begin.clone(); end.set(Calendar.HOUR_OF_DAY, endHour);
if (context == null) { Log.e("Calendar", "Context is null"); return; }
CalendarHelper.addEventWithPermission( (Activity) context, title, description, location, begin.getTimeInMillis(), end.getTimeInMillis(), 5 ); } catch (JSONException e) { e.printStackTrace(); Log.e("Calendar", "JSON解析失败:" + data); } }
public static void setDenyPermissionMessage(String message) { if (!TextUtils.isEmpty(message)) { denyPermissionMessage = message; } } }
|