Share Coding

Tutorials, Problems, Stuffs …

Android google calendar API getting calendar list from phone

First of all, to communicate with google service directly has limitation. Courtesy limit: 10,000 requests/day
We get data from the phone is the best choice.

1. Download dependency jar:

Get the google-api-client-1.4.1-beta.jar


/libs/android-support-v4.jar should always with you, and put all of those jar file into /lib/

1.1 Setup the jar work with the project
Go to project properties, library, add JARs, go /lib/ and select all of them

2. Coding at main

package com.example.clientlogintest;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView mylist;
    private AccountManager manager;
    private Account[] accounts;
    private Account acc;

    protected void onCreate(Bundle savedInstanceState) {


        GoogleAccountManager googleAccountManager = new GoogleAccountManager(this);
        accounts = googleAccountManager.getAccounts();

        /* Create an array adapter to store google account name */
        ArrayAdapter < String > adapter = new ArrayAdapter < String > (this,

        for (int i = 0; i < accounts.length; i++) {
            Log.d("Debug", accounts[i].name + "type: " + accounts[i].type
            + "desc: " + accounts[i].describeContents());

        mylist = (ListView) findViewById(;
        mylist.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView <? > parent, View view,
            int position, long id) {
                Log.d("Debug", "You select" + accounts[position].name);
                acc = accounts[position];

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public void getCalendarList() {
        final Cursor cursor;
        ContentResolver contentResolver = this.getContentResolver();
        if (Build.VERSION.SDK_INT >= 8) {
            cursor = contentResolver.query(Uri.parse("content: //"), null, null, null, null);
            } else {
                cursor = contentResolver.query(Uri.parse("content: //calendar/calendars"), null, null, null, null);

            System.out.println("Count = " + cursor.getCount());
            if (cursor.getCount() > 0) {
                System.out.println("the control is just inside of the cursor.count loop");
                while (cursor.moveToNext()) {
                    String _id = cursor.getString(0);
                    String displayName = cursor.getString(1);
                    Boolean selected = !cursor.getString(2).equals("0");
                    Log.d("Debug3", "Id: " + _id + "Display Name: " + displayName + "Selected: " + selected);


4 responses to “Android google calendar API getting calendar list from phone

  1. Cory 2013/06/10 at 1:52 am

    Hi there to every body, it’s my first pay a visit of this blog; this webpage carries amazing and genuinely fine material for readers.

  2. sudhakar 2013/10/09 at 6:20 pm

    i am geting null pointer exception in cursor.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: