Wednesday, March 4, 2015

Menggunakan Calendar Provider API Android

Calendar Provider API biasanya digunakan untuk membuat event. Menggunakan Calendar Provider API kita dapat membuat, mengubah dan menghapus event. Yang akan dibuat kali ini hanya create event saja.

1. XML Layout
Buat dua XML layout file di folder “res/layout/” :

res/layout/activity_main.xml tombol untuk membuat event

File : activity_main.xml
<LinearLayout 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:orientation="vertical" >"

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Create Event" />

</LinearLayout>

2. Activity
Buat sebuah activity class.

1. MainActivity.java

File : MainActivity.java
package com.example.calendarapi;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    
 private static final String TAG = "MainActivity";
 
    public static final String[] EVENT_PROJECTION = new String[] {
  Calendars._ID,      // 0
  Calendars.ACCOUNT_NAME,   // 1
  Calendars.CALENDAR_DISPLAY_NAME // 2
    };
  private static final int PROJECTION_ID_INDEX = 0;
  private static final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
  private static final int PROJECTION_DISPLAY_NAME_INDEX = 2;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
  public void onClick(View view) {
  Intent intent = new Intent(Intent.ACTION_INSERT);
  intent.setType  ("vnd.android.cursor.item/event");
  intent.putExtra  (Events.TITLE, "Learn Android");
  intent.putExtra  (Events.EVENT_LOCATION, "Jakarta, Indonesia");
  intent.putExtra  (Events.DESCRIPTION, "");
  GregorianCalendar calDate = new GregorianCalendar();
  intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
    calDate.getTimeInMillis());
  intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
    calDate.getTimeInMillis());
  intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
  intent.putExtra(Events.RRULE,
    "FREQ=WEEKLY;COUNT=11;WKST=SU;BYDAY=TU,TH");
  intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
  intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);
  startActivity(intent);
 }  

  public void queryCalendar(View view) {
  Cursor cursor = null;
  ContentResolver cr = getContentResolver();
  Uri uri = Calendars.CONTENT_URI;
  String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND ("
    + Calendars.ACCOUNT_TYPE + " = ?))";
  String[] 
    selectionArgs = new String[] { "sugarhutabarat@gmail.com",
    "com.google" };
  cursor = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
  Toast.makeText(this, String.valueOf(cursor.getCount()), Toast.LENGTH_LONG)
    .show();
  
  while (cursor.moveToNext()) {
   long calID = 0;
   String displayName = null;
   String accountName = null;
   
   calID   = cursor.getLong(PROJECTION_ID_INDEX);
   displayName = cursor.getString(PROJECTION_DISPLAY_NAME_INDEX);
   accountName = cursor.getString(PROJECTION_ACCOUNT_NAME_INDEX);
   
   Toast.makeText(this, "Calendar " + displayName, Toast.LENGTH_SHORT)
     .show();
   
   long calID1 = 2;
   ContentValues 
   values = new ContentValues();
   values.put(Calendars.CALENDAR_DISPLAY_NAME, "Your Calendar Name");
   
   Uri updateUri = ContentUris.withAppendedId(Calendars.CONTENT_URI, calID1);
   int rows = getContentResolver().update(updateUri, values, null, null);
   Log.i(TAG, "Rows updated: " + rows);
  }
 }
}


Download Source Code
download source code disini dan cara download tusfile disini

References 


S.M.H

No comments:

Post a Comment