Android 获取日历本地账号的demo
时间: 2023-11-10 08:17:39 浏览: 218
以下是获取 Android 日历本地账号的示例代码:
```java
// 获取日历账户
private String getCalendarAccount() {
String account = "";
Cursor cursor = null;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
cursor = getContentResolver().query(CalendarContract.Calendars.CONTENT_URI,
new String[]{CalendarContract.Calendars._ID, CalendarContract.Calendars.ACCOUNT_NAME},
CalendarContract.Calendars.ACCOUNT_TYPE + " = ? AND " + CalendarContract.Calendars.OWNER_ACCOUNT + " = ?",
new String[]{"com.android.exchange", "your.email@example.com"}, null);
} else {
cursor = getContentResolver().query(CalendarContract.Calendars.CONTENT_URI,
new String[]{CalendarContract.Calendars._ID, CalendarContract.Calendars.ACCOUNT_NAME},
CalendarContract.Calendars.ACCOUNT_TYPE + " = ?",
new String[]{"com.android.exchange"}, null);
}
if (cursor != null && cursor.moveToFirst()) {
int accountIndex = cursor.getColumnIndex(CalendarContract.Calendars.ACCOUNT_NAME);
account = cursor.getString(accountIndex);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return account;
}
```
其中,`CalendarContract.Calendars.CONTENT_URI` 是日历的内容 URI,`CalendarContract.Calendars._ID` 是日历 ID,`CalendarContract.Calendars.ACCOUNT_NAME` 是日历账户名称,`CalendarContract.Calendars.ACCOUNT_TYPE` 是日历账户类型,`CalendarContract.Calendars.OWNER_ACCOUNT` 是日历所有者账户。根据实际情况修改参数即可。
阅读全文