从Google Calendar取出Scheduler

设定日历数据类

public class Scheduler {
	/**
	 * event id
	 */
	private long eventID;
	/**
	 * the scheduler's date, like "MM/dd/yyyy"
	 */
	private String date;
	/**
	 * the value of begin time
	 */
	private String beginTime;
	/**
	 * the value of end time
	 */
	private String endTime;
	/**
	 * the scheduler's title
	 */
	private String title;
	/**
	 * the scheduler's description 
	 */
	private String description;
	
	
	
	public long getEventID() {
		return eventID;
	}
	public void setEventID(long eventID) {
		this.eventID = eventID;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getBeginTime() {
		return beginTime;
	}
	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}
	public String getEndTime() {
		return endTime;
	}
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
}


建立查找类(实现取得当天的计划)

直接能用,接口做了sdk版本控制,4.0一下的sdk不支持该Calendar,会直接返回null

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract.Instances;

/**
 * quert scheduler from google calendar today
 * @author zoeice
 *
 */
public class QueryScheduler {
	public static final String[] INSTANCE_PROJECTION = new String[] {
	    Instances.EVENT_ID,      // 0
	    Instances.BEGIN,         // 1
	    Instances.END,	 		//2
	    Instances.TITLE,          // 3
	    Instances.DESCRIPTION		//4
	  };
	// The indices for the projection array above.
	private static final int PROJECTION_ID_INDEX = 0;
	private static final int PROJECTION_BEGIN_INDEX = 1;
	private static final int PROJECTION_END_INDEX = 2;
	private static final int PROJECTION_TITLE_INDEX = 3;
	private static final int PROJECTION_DESCRIPTION_INDEX = 4;
	
	private Context context;
	
	public QueryScheduler(Context context) {
		super();
		this.context = context;
	}

	/**
	 * query scheduler only today
	 */
	public ArrayList<Scheduler> queryGoogleCalendar(){
		int sdkVersion = android.os.Build.VERSION.SDK_INT;
		if(sdkVersion >= 14){
			ArrayList<Scheduler> list = new ArrayList<Scheduler>();
			// Specify the date range you want to search for recurring
			// event instances
			
			Calendar beginTime = Calendar.getInstance();
			beginTime.set(beginTime.get(Calendar.YEAR), beginTime.get(Calendar.MONTH), beginTime.get(Calendar.DAY_OF_MONTH) , 0, 0);
			long startMillis = beginTime.getTimeInMillis();
			
			Calendar endTime = Calendar.getInstance();
			endTime.set(endTime.get(Calendar.YEAR), endTime.get(Calendar.MONTH), endTime.get(Calendar.DAY_OF_MONTH) , 23, 59);
			long endMillis = endTime.getTimeInMillis();
			  
			Cursor cur = null;
			ContentResolver cr = context.getContentResolver();
	
			// Construct the query with the desired date range.
			Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
			ContentUris.appendId(builder, startMillis);
			ContentUris.appendId(builder, endMillis);
	
			// Submit the query
			cur =  cr.query(builder.build(), 
			    INSTANCE_PROJECTION, 
			    null, 
			    null, 
			    null);
			   
			while (cur.moveToNext()) {
				
			    String title = null;
			    String description = null;
			    long eventID = 0;
			    long beginVal = 0;    
			    long endVal = 0;
			    
			    // Get the field values
			    eventID = cur.getLong(PROJECTION_ID_INDEX);
			    beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
			    endVal = cur.getLong(PROJECTION_END_INDEX);
			    title = cur.getString(PROJECTION_TITLE_INDEX);
			    description = cur.getString(PROJECTION_DESCRIPTION_INDEX);
			    
			    Calendar calendar = Calendar.getInstance();
			    calendar.setTimeInMillis(beginVal);  
			    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
			    
			    Scheduler scheduler = new Scheduler();
			    scheduler.setEventID(eventID);
			    scheduler.setBeginTime(formatter.format(calendar.getTime()).substring(11));
			    
			    calendar.setTimeInMillis(endVal);  
			    scheduler.setEndTime(formatter.format(calendar.getTime()).substring(11));
			    
			    scheduler.setTitle(title);
			    scheduler.setDescription(description);
			    scheduler.setDate(formatter.format(calendar.getTime()).substring(0, 10));
			    list.add(scheduler);
		    }
			if(list.size() > 0){
				return list;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
}


通过google账户查询的话用如下方法:

// Projection array. Creating indices for this array instead of doing
// dynamic lookups improves performance.
public static final String[] EVENT_PROJECTION = new String[] {
    Calendars._ID,                           // 0
    Calendars.ACCOUNT_NAME,                  // 1
    Calendars.CALENDAR_DISPLAY_NAME,         // 2
    Calendars.OWNER_ACCOUNT                  // 3
};
  
// The indices for the projection array above.
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;
private static final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;

// Run query
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = Calendars.CONTENT_URI;   
String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND (" 
                        + Calendars.ACCOUNT_TYPE + " = ?) AND ("
                        + Calendars.OWNER_ACCOUNT + " = ?))";
String[] selectionArgs = new String[] {"sampleuser@gmail.com", "com.google",
        "sampleuser@gmail.com"}; 
// Submit the query and get a Cursor object back. 
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);


往Calendars插入事件详细api查询http://developer.android.com/guide/topics/providers/calendar-provider.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值