如何整合日历活动

时间:2013-10-11 05:55:38

标签: android google-calendar-api

我需要在我的应用中集成谷歌日历事件(CRUD操作)。首先,我想到使用内容提供程序从本地日历应用程序访问事件数据。但我发现只有ICS支持日历提供商(api 14)。 1)有没有办法在较低版本中访问日历提供程序? 2)是否可以在android中使用谷歌日历api(https://developers.google.com/google-apps/calendar/)?

3 个答案:

答案 0 :(得分:1)

要获取日历事件,请检查以下代码是否有效:

private void getCalenderEvents() {
        Cursor cursor = context.getContentResolver().query(Uri.parse("content://com.android.calendar/events"),
                new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null);
        cursor.moveToFirst();
        // fetching calendars name
        String CNames[] = new String[cursor.getCount()];

        for (int i = 0; i < CNames.length; i++) {

            rowDataEvents = new HashMap<String, String>();

            String eventTitle = cursor.getString(1);
            String eventStartDate = cursor.getString(3);
            String eventEndDate = cursor.getString(4);
            String eventDescription = cursor.getString(2);
            String eventLocation = cursor.getString(5);
            cursor.moveToNext();

        }
        cursor.close();
    }

答案 1 :(得分:0)

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Calendar;

public class MainActivity extends Activity {

    Button disp,add;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        disp = (Button) findViewById(R.id.dispbut);
        add = (Button) findViewById(R.id.btn1);
        disp.setOnClickListener(new View.OnClickListener()



        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                disp();
            }
        });


        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                add();
            }


        });
    }

    public void disp() {
        Calendar startTime = Calendar.getInstance();
        startTime.set(2013, 2, 13, 11, 35);
        Uri uri = Uri.parse("content://com.android.calendar/time/"
                + String.valueOf(startTime.getTimeInMillis()));
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        // Use the Calendar app to view the time.
        startActivity(intent);
    }

    public void add(){

        Calendar cal = Calendar.getInstance();
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", cal.getTimeInMillis());
        intent.putExtra("allDay", true);
        intent.putExtra("rrule", "FREQ=YEARLY");
        intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
        intent.putExtra("title", "Test Event");
        intent.putExtra("description", "This is a sample description");
        startActivity(intent);
    }



} 

答案 2 :(得分:-1)

<uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/dispbut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Display Event" />
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add An Event" />

</RelativeLayout>