get Calendar Id - Android Android OS

Android examples for Android OS:Calendar Contract

Description

get Calendar Id

Demo Code


//package com.java2s;
import android.annotation.SuppressLint;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;

public class Main {
    @SuppressLint("NewApi")
    public static long getCalendarId(Context context) {
        long calId = 0;

        Uri calendars;/*from w  w  w  .jav  a2 s  . c  o  m*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            calendars = CalendarContract.Calendars.CONTENT_URI;
        } else {
            calendars = Uri.parse(getContentUri() + "/calendars");
        }

        Cursor managedCursor = context.getContentResolver()
                .query(calendars, new String[] { "_id", "name" }, null,
                        null, null);
        if (managedCursor.moveToFirst()) {
            calId = managedCursor.getLong(managedCursor
                    .getColumnIndex("_id"));
        }
        managedCursor.close();

        return calId;
    }

    @SuppressLint("NewApi")
    private static String getContentUri() {
        String AUTHORITY = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            AUTHORITY = "com.android.calendar";
        } else {
            AUTHORITY = "calendar";
        }
        return "content://" + AUTHORITY;
    }
}

Related Tutorials