一、创建定时器
设置权限
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
案例
https://developer.android.com/reference/android/provider/AlarmClock.html#EXTRA\_MESSAGE
public void createAlarm(String message, int hour, int minutes) { Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM) .putExtra(AlarmClock.EXTRA_MESSAGE, message) .putExtra(AlarmClock.EXTRA_HOUR, hour) .putExtra(AlarmClock.EXTRA_MINUTES, minutes); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
可重复闹铃
EXTRA_DAYS
一个 ArrayList,其中包括应重复触发该闹铃的每个周日。 每一天都必须使用 Calendar 类中的某个整型值(如 MONDAY)进行声明。
对于一次性闹铃,无需指定此 extra。
参考
https://developer.android.google.cn/guide/components/intents-common.html?hl=zh-cn#Clock
创建系统定时器
public void startTimer(String message, int seconds) { Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER) .putExtra(AlarmClock.EXTRA_MESSAGE, message) .putExtra(AlarmClock.EXTRA_LENGTH, seconds) .putExtra(AlarmClock.EXTRA_SKIP_UI, true); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
二、添加日历事件
添加权限
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
添加日历事件
https://developer.android.com/reference/android/provider/AlarmClock.html#EXTRA\_MESSAGE
public void addEvent(String title, String location, Calendar begin, Calendar end) { Intent intent = new Intent(Intent.ACTION_INSERT) .setData(Events.CONTENT_URI) .putExtra(Events.TITLE, title) .putExtra(Events.EVENT_LOCATION, location) .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin) .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
三、通过ContentProvider设置日历
uri matcher
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";
插入
/**
* 增加日历提醒事件
* @param activity
* @param dataEntity
* @param isRecord
* @return
*/
public static boolean addCalendarReminder(Activity activity,DataEntity dataEntity, boolean isRecord) {
if (!AactivityCompat.hasPermissions(activity, Manifest.permission.WRITE_CALENDAR, Manifest.permission.READ_CALENDAR)) {
return false;
}
try {
// Calendar startCalendar = Calendar.getInstance();
// long startTime = startCalendar.getTimeInMillis() + 8 * 60 * 1000;
long startTime = dataEntity.getRemind_time();
// Calendar endCalendar = Calendar.getInstance();
// long endTime = endCalendar.getTimeInMillis() + 50 * 60 * 1000;
long endTime = startTime + 10 * 60 * 1000;
String remindTitle = dataEntity.getTitle();
// 日历事件
ContentValues calEvent = new ContentValues();
calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); // XXX pick)
calEvent.put(CalendarContract.Events.TITLE, remindTitle);
calEvent.put(CalendarContract.Events.DTSTART, startTime);
calEvent.put(CalendarContract.Events.DTEND, endTime);
calEvent.put(CalendarContract.Events.HAS_ALARM, 1);
calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, CalendarContract.Calendars.CALENDAR_TIME_ZONE);
ContentResolver cr = activity.getContentResolver();
if (null == cr) {
return false;
}
try {
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, calEvent);
if (null == uri) {
return false;
}
int eventId = Integer.parseInt(uri.getLastPathSegment());
/** 记录code 记录eventId **/
if (isRecord) {
saveEventCode(dataEntity.getProduct_code());
saveEventId(eventId);
}
// 日历提醒
ContentValues reminders = new ContentValues();
reminders.put(CalendarContract.Reminders.EVENT_ID, eventId);
reminders.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
reminders.put(CalendarContract.Reminders.MINUTES, REMIND_MINUTES);
cr.insert(CalendarContract.Reminders.CONTENT_URI, reminders);
return true;
} catch (SecurityException ignore) {}
} catch (Exception ignore) {}
return false;
}
参考:http://blog.csdn.net/zhaoshuyu111/article/details/53195142