如何通过Android日历api插入日程(事件)和提醒(通知)
Posted zhangmingcsdn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过Android日历api插入日程(事件)和提醒(通知)相关的知识,希望对你有一定的参考价值。
由于全是api级操作,这里直接贴代码。按照从上到下循序看完就可以
参考文档:https://developer.android.google.cn/guide/topics/providers/calendar-provider?hl=zh-cn
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Reminders;
import android.util.Log;
import android.view.View;
/**
* 流程:创建日历->根据日历id创建日程(事件)->创建日程(事件)的提醒时间
* author: simenHi
* create time: 2020-10-30 10:02
* description:
* 参考文档:https://developer.android.google.cn/guide/topics/providers/calendar-provider?hl=zh-cn
*/
public class TestCalendarActivity extends Activity
private static final String TAG = TestCalendarActivity.class.getSimpleName();
public static final String MY_ACCOUNT_NAME = "Test";
public static final String CALENDAR_NAME = "Test Calendar";
public static final String CALENDAR_TIME_ZONE = "Asia/Shanghai";
public static final String OWNER_ACCOUNT = "test@qq.com";
public static final String MY_TITLE = "My Title";
public static final String DESCRIPTION = "内容描述";
public static final String LOCATION = "位置";
public static final String RELATIVE_EMAIL = "test1@example.com,test2@example.com";
private long calendarId;
private long eventID;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testcalendar);
/**
* 打印所有日历
*/
public void listAccount(View view)
List<CalendarItem> list = getCalendars(this);
log("listAccount: " + JSON.toJSONString(list));
/**
* 创建日历
*/
public void createCalendar(View view)
createNewCalendar();
public void getCalendarId(View view)
calendarId = getCalendarId();
log("calendarId: " + calendarId);
/**
* 创建日历
*/
private void createNewCalendar()
long calendarId = getCalendarId();
if (calendarId != -1)
Log.w(TAG, String.format("createNewCalendar: calendar %1$s has exist", CALENDAR_NAME));
return;
ContentValues values = new ContentValues();
values.put(Calendars.ACCOUNT_NAME, MY_ACCOUNT_NAME);
values.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
values.put(Calendars.NAME, CALENDAR_NAME);
values.put(Calendars.CALENDAR_DISPLAY_NAME, CALENDAR_NAME);
values.put(Calendars.CALENDAR_COLOR, 0xFFFF0000);
values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
values.put(Calendars.OWNER_ACCOUNT, OWNER_ACCOUNT);
values.put(Calendars.CALENDAR_TIME_ZONE, CALENDAR_TIME_ZONE);
values.put(Calendars.SYNC_EVENTS, 1);
Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
builder.appendQueryParameter(Calendars.ACCOUNT_NAME, MY_ACCOUNT_NAME);
builder.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
builder.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true");
Uri uri = getContentResolver().insert(builder.build(), values);
Log.i(TAG, "createNewCalendar: " + uri);
/**
* @return 日历id
*/
private long getCalendarId()
String[] projection = new String[]Calendars._ID;
String selection = Calendars.ACCOUNT_NAME + " = ? ";
String[] selArgs = new String[]MY_ACCOUNT_NAME;
Cursor cursor = getContentResolver().query(
Calendars.CONTENT_URI,
projection,
selection,
selArgs,
null);
if (cursor != null)
if (cursor.moveToFirst()) return cursor.getLong(0);
cursor.close();
return -1;
/**
* 创建日程和提醒
*/
public void testInsertEvent(View v)
if (calendarId == 0)
ToastUtil.showMessage(this, "请创建日历,并获取日历id");
return;
long calID = calendarId;
long eventID;
long millis = System.currentTimeMillis();
long startMillis = millis + 1 * 1000 * 60;
long endMillis = millis + 3 * 1000 * 60;
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, MY_TITLE);
values.put(Events.DESCRIPTION, DESCRIPTION);
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, CALENDAR_TIME_ZONE);
Uri uri = cr.insert(Events.CONTENT_URI, values);
if (uri.getLastPathSegment() != null && uri != null)
// get the event ID that is the last element in the Uri
eventID = Long.parseLong(uri.getLastPathSegment());
else
log("插入事件失败");
return;
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Reminders.MINUTES, 0); //提醒会在事件发生前 0 分钟发出
values.put(Reminders.EVENT_ID, eventID);
values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
Uri uri = cr.insert(Reminders.CONTENT_URI, values);
if (uri != null)
log("插入提醒成功 " + eventID);
this.eventID = eventID;
else
log("插入提醒失败");
/**
* 删除所有自己创建的日历
*/
public void delCalendar(View view)
Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
builder.appendQueryParameter(Calendars.ACCOUNT_NAME, MY_ACCOUNT_NAME);
builder.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
builder.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true");
int affect = getContentResolver().delete(builder.build(), Calendars.NAME + "=?", new String[]CALENDAR_NAME);
if (affect > 0)
ToastUtil.showMessage(this, "删除成功 " + affect);
else
ToastUtil.showMessage(this, "删除失败");
/**
* 编辑日程(通过Intent)
*/
public void testEditEventByIntent(View view)
Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
Intent intent = new Intent(Intent.ACTION_EDIT)
.setData(uri)
.putExtra(Events.TITLE, MY_TITLE);
startActivity(intent);
/**
* 创建日程(通过Intent)
*/
public void testInsertEventByIntent(View view)
long timeMillis = System.currentTimeMillis() + 3 * 60 * 1000;
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, timeMillis)
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, timeMillis + 3 * 60 * 1000)
.putExtra(Calendars.SYNC_EVENTS, 1)
.putExtra(Events.TITLE, MY_TITLE)
.putExtra(Events.DESCRIPTION, DESCRIPTION)
.putExtra(Events.EVENT_LOCATION, LOCATION)
.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
.putExtra(Intent.EXTRA_EMAIL, RELATIVE_EMAIL);
startActivity(intent);
public List<CalendarItem> getCalendars(Context ctx)
List<CalendarItem> result = new ArrayList();
final String[] EVENT_PROJECTION = new String[]
Calendars._ID,
Calendars.NAME
;
final ContentResolver cr = ctx.getContentResolver();
final Uri uri = Calendars.CONTENT_URI;
Cursor cur = cr.query(uri, EVENT_PROJECTION, null, null, null);
if(cur!=null)
while (cur.moveToNext())
CalendarItem item = new CalendarItem();
item.setId(cur.getInt(cur.getColumnIndex(Calendars._ID)));
item.setName(cur.getString(cur.getColumnIndex(Calendars.NAME)));
result.add(item);
cur.close();
return result;
public void getPermissions(View view)
PermissionHelper.getHelper()
.requestPermission(this,
Manifest.permission.WRITE_CALENDAR,
Manifest.permission.READ_CALENDAR)
.setListener(new PermissionListener()
@Override
public void onPermissionRequestResult(boolean allGranted)
if (allGranted)
ToastUtil.showMessage(TestCalendarActivity.this, "获取权限成功");
else
ToastUtil.showMessage(TestCalendarActivity.this, "没有获取权限");
).request();
public void log(String msg)
Log.i(TAG, "log: " + msg);
public class CalendarItem
public int id;
public String name;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
以上是关于如何通过Android日历api插入日程(事件)和提醒(通知)的主要内容,如果未能解决你的问题,请参考以下文章