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: http://code.google.com/p/google-api-java-client/

Get the google-api-client-1.4.1-beta.jar
http://code.google.com/p/google-api-java-client/wiki/APIs#Calendar_API

Get
google-api-client-extensions-android2-1.4.1-beta.jar
google-api-client-extensions-android3-1.4.1-beta.jar
google-api-client-googleapis-1.4.1-beta.jar
google-api-client-googleapis-extensions-android2-1.4.1-beta.jar
google-api-services-calendar-v3-rev23-1.13.2-beta.jar

Also, 
/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 com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;
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.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        /* Create an array adapter to store google account name */
        ArrayAdapter < String > adapter = new ArrayAdapter < String > (this,
        android.R.layout.simple_list_item_1);

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

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, 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: //com.android.calendar/calendars"), 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);
                }
            }
        }

    }
Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: