下面列出了怎么用android.provider.CalendarContract.Calendars的API类实例代码及写法,或者点击链接到github查看源代码。
private ArraySet<Long> getPrimaryCalendars() {
final long start = System.currentTimeMillis();
final ArraySet<Long> rt = new ArraySet<>();
final String primary = "\"primary\"";
final String[] projection = { Calendars._ID,
"(" + Calendars.ACCOUNT_NAME + "=" + Calendars.OWNER_ACCOUNT + ") AS " + primary };
final String selection = primary + " = 1";
Cursor cursor = null;
try {
cursor = mUserContext.getContentResolver().query(Calendars.CONTENT_URI, projection,
selection, null, null);
while (cursor != null && cursor.moveToNext()) {
rt.add(cursor.getLong(0));
}
} finally {
if (cursor != null) {
cursor.close();
}
}
if (DEBUG) Log.d(TAG, "getPrimaryCalendars took " + (System.currentTimeMillis() - start));
return rt;
}
private void setRegistered(boolean registered) {
if (mRegistered == registered) return;
final ContentResolver cr = mSystemContext.getContentResolver();
final int userId = mUserContext.getUserId();
if (mRegistered) {
if (DEBUG) Log.d(TAG, "unregister content observer u=" + userId);
cr.unregisterContentObserver(mObserver);
}
mRegistered = registered;
if (DEBUG) Log.d(TAG, "mRegistered = " + registered + " u=" + userId);
if (mRegistered) {
if (DEBUG) Log.d(TAG, "register content observer u=" + userId);
cr.registerContentObserver(Instances.CONTENT_URI, true, mObserver, userId);
cr.registerContentObserver(Events.CONTENT_URI, true, mObserver, userId);
cr.registerContentObserver(Calendars.CONTENT_URI, true, mObserver, userId);
}
}
private long getCalendarId() {
String[] projection = new String[]{Calendars._ID};
//String selection = Calendars.ACCOUNT_NAME + "=Biermacht AND" + Calendars.ACCOUNT_TYPE + "=" + CalendarContract.ACCOUNT_TYPE_LOCAL;
String selection = "(" + Calendars.ACCOUNT_NAME + " = ?) AND (" + Calendars.ACCOUNT_TYPE + " = ?)";
String[] selectionArgs = new String[]{"Biermacht", CalendarContract.ACCOUNT_TYPE_LOCAL};
// use the same values as above:
//String[] selArgs = new String[]{"Biermacht", CalendarContract.ACCOUNT_TYPE_LOCAL};
Cursor cursor = c.getContentResolver().query(Calendars.CONTENT_URI,
projection,
selection,
selectionArgs,
null);
if (cursor.moveToFirst()) {
return cursor.getLong(0);
}
return - 1;
}
private void createCalendar() {
ContentValues values = new ContentValues();
values.put(Calendars.ACCOUNT_NAME, "Biermacht");
values.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
values.put(Calendars.NAME, "Biermacht Calendar");
values.put(Calendars.CALENDAR_DISPLAY_NAME, "Biermacht Calendar");
values.put(Calendars.CALENDAR_COLOR, 0xE6A627);
values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
values.put(Calendars.OWNER_ACCOUNT, "[email protected]");
values.put(Calendars.CALENDAR_TIME_ZONE, "Europe/Berlin");
Uri.Builder builder = CalendarContract.Calendars.CONTENT_URI.buildUpon();
builder.appendQueryParameter(Calendars.ACCOUNT_NAME, "com.biermacht.brews");
builder.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
builder.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true");
Uri uri = c.getContentResolver().insert(builder.build(), values);
}
@SuppressLint("MissingPermission")
static String getSummary(String value, Context context) {
String summary = context.getString(R.string.calendars_multiselect_summary_text_not_selected);
if (Permissions.checkCalendar(context)) {
if (!value.isEmpty()) {
String[] splits = value.split("\\|");
if (splits.length == 1) {
boolean found = false;
Cursor cur;
ContentResolver cr = context.getContentResolver();
Uri uri = Calendars.CONTENT_URI;
String selection = Calendars._ID + "=" + splits[0];
// permission is already checked in Permissions.checkCalendar()
//noinspection MissingPermission
cur = cr.query(uri, CalendarsMultiSelectDialogPreferenceFragmentX.CALENDAR_PROJECTION, selection, null, null);
if (cur != null) {
//while (cur.moveToNext()) {
if (cur.moveToFirst()) {
found = true;
summary = cur.getString(CalendarsMultiSelectDialogPreferenceFragmentX.PROJECTION_DISPLAY_NAME_INDEX);
//break;
}
cur.close();
}
if (!found)
summary = context.getString(R.string.calendars_multiselect_summary_text_selected) + ": " + splits.length;
} else
summary = context.getString(R.string.calendars_multiselect_summary_text_selected) + ": " + splits.length;
}
}
return summary;
}
public void queryCalendar(View view) {
// Run query
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = Calendars.CONTENT_URI;
String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND ("
+ Calendars.ACCOUNT_TYPE + " = ?))";
// Replace this with your own user and account type
String[] selectionArgs = new String[] { "[email protected]",
"com.google" };
// Submit the query and get a Cursor object back.
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
Toast.makeText(this, String.valueOf(cur.getCount()), Toast.LENGTH_LONG)
.show();
// Use the cursor to step through the returned records
while (cur.moveToNext()) {
long calID = 0;
String displayName = null;
String accountName = null;
// Get the field values
calID = cur.getLong(PROJECTION_ID_INDEX);
displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
// Do something with the values...
Toast.makeText(this, "Calendar " + displayName, Toast.LENGTH_SHORT)
.show();
}
}
/**
* Checks the server for an updated list of Calendars (in the background).
* <p/>
* If a Calendar is added on the web (and it is selected and not
* hidden) then it will be added to the list of calendars on the phone
* (when this finishes). When a new calendar from the
* web is added to the phone, then the events for that calendar are also
* downloaded from the web.
* <p/>
* This sync is done automatically in the background when the
* SelectCalendars activity and fragment are started.
*
* @param account - The account to sync. May be null to sync all accounts.
*/
public static void startCalendarMetafeedSync(Account account) {
Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
extras.putBoolean("metafeedonly", true);
ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras);
}