package com.a.a.utils;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.provider.CalendarContract;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
/**
* 提醒
* @author Administrator
*
*/
public class EventReminderUtils
{
public EventReminderUtils(){}
private static String CALANDER_URL = "content://com.android.calendar/calendars";
private static String CALANDER_EVENT_URL = "content://com.android.calendar/events";
private static String CALANDER_REMIDER_URL = "content://com.android.calendar/reminders";
private static String CALENDARS_NAME = "table";
private static String CALENDARS_ACCOUNT_NAME = "table@table.com";
private static String CALENDARS_ACCOUNT_TYPE = "com.android.table";
private static String CALENDARS_DISPLAY_NAME = "帅帅";
public static String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm";
//删除
public static void deleteCalendarEvent(Context context){
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALANDER_EVENT_URL), null, null, null, null);
try {
if (eventCursor == null)//查询返回空值
return;
if (eventCursor.getCount() > 0) {
//遍历所有事件,找到title跟需要查询的title一样的项
for (eventCursor.moveToFirst(); !eventCursor.isAfterLast(); eventCursor.moveToNext()) {
String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
if (eventTitle.startsWith("标题提醒")) {
int id = eventCursor.getInt(eventCursor.getColumnIndex(CalendarContract.Calendars._ID));//取得id
Uri deleteUri = ContentUris.withAppendedId(Uri.parse(CALANDER_EVENT_URL), id);
int rows = context.getContentResolver().delete(deleteUri, null, null);
if (rows == -1) {
//事件删除失败
return;
}
}
}
}
} finally {
if (eventCursor != null) {
eventCursor.close();
}
}
}
public static boolean selectCalendarEvent(Context context,String data){
Cursor eventCursor = context.getContentResolver().query(Uri.parse(CALANDER_EVENT_URL), null, null, null, null);
try {
if (eventCursor == null)//查询返回空值
return false;
if (eventCursor.getCount() > 0) {
//遍历所有事件,找到title跟需要查询的title一样的项
for (eventCursor.moveToFirst(); !eventCursor.isAfterLast(); eventCursor.moveToNext()) {
String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
if (eventTitle.equals("标题提醒"+data)) {
return true;
}
}
}
return false;
} finally {
if (eventCursor != null) {
eventCursor.close();
}
}
}
/**
* 添加事件
* @param context
*/
public static void addCalendarEvent(Context context,String data){
// 获取日历账户的id
int calId = checkAndAddCalendarAccount(context);
if (calId < 0) {
// 获取账户id失败直接返回,添加日历事件失败
return;
}
ContentValues event = new ContentValues();
event.put("title", "标题提醒"+data);
event.put("description", "您有一个美女未领,请尽快领取!");
// 插入账户的id
event.put("calendar_id", calId);
long start = getDateLong(data+" 9:00");
event.put(CalendarContract.Events.DTSTART, start);
event.put(CalendarContract.Events.DTEND, start+10*1000);
event.put(CalendarContract.Events.HAS_ALARM, 1);//设置有闹钟提醒
event.put(CalendarContract.Events.EVENT_TIMEZONE, "Asia/Shanghai"); //这个是时区,必须有,
//添加事件
Uri newEvent = context.getContentResolver().insert(Uri.parse(CALANDER_EVENT_URL), event);
if (newEvent == null) {
// 添加日历事件失败直接返回
return;
}
//事件提醒的设定
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
// 提前10分钟有提醒
values.put(CalendarContract.Reminders.MINUTES, 10);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = context.getContentResolver().insert(Uri.parse(CALANDER_REMIDER_URL), values);
if(uri == null) {
// 添加闹钟提醒失败直接返回
return;
}
}
//检查是否已经添加了日历账户,如果没有添加先添加一个日历账户再查询
private static int checkAndAddCalendarAccount(Context context){
int oldId = checkCalendarAccount(context);
if( oldId >= 0 ){
return oldId;
}else{
long addId = addCalendarAccount(context);
if (addId >= 0) {
return checkCalendarAccount(context);
} else {
return -1;
}
}
}
private static int checkCalendarAccount(Context context) {
Cursor userCursor = context.getContentResolver().query(Uri.parse(CALANDER_URL), null, null, null, null);
try {
if (userCursor == null)//查询返回空值
return -1;
int count = userCursor.getCount();
if (count > 0) {//存在现有账户,取第一个账户的id返回
userCursor.moveToFirst();
return userCursor.getInt(userCursor.getColumnIndex(CalendarContract.Calendars._ID));
} else {
return -1;
}
} finally {
if (userCursor != null) {
userCursor.close();
}
}
}
private static long addCalendarAccount(Context context) {
TimeZone timeZone = TimeZone.getDefault();
ContentValues value = new ContentValues();
value.put(CalendarContract.Calendars.NAME, CALENDARS_NAME);
value.put(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME);
value.put(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE);
value.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CALENDARS_DISPLAY_NAME);
value.put(CalendarContract.Calendars.VISIBLE, 1);
value.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.BLUE);
value.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
value.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
value.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, timeZone.getID());
value.put(CalendarContract.Calendars.OWNER_ACCOUNT, CALENDARS_ACCOUNT_NAME);
value.put(CalendarContract.Calendars.CAN_ORGANIZER_RESPOND, 0);
Uri calendarUri = Uri.parse(CALANDER_URL);
calendarUri = calendarUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE)
.build();
Uri result = context.getContentResolver().insert(calendarUri, value);
long id = result == null ? -1 : ContentUris.parseId(result);
return id;
}
/**
* 日期格式字符串转换成时间戳
*
* @return
*/
public static long getDateLong(String date_str) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT_PATTERN);
return sdf.parse(date_str).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}