安卓(Android) 怎么创建新日历并将其插入android数据库

2019年10月30日21:47:16安卓(Android) 怎么创建新日历并将其插入android数据库已关闭评论 174 2107字阅读7分1秒

1.简介

安卓(Android) 怎么创建新日历并将其插入android数据库?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Attendees;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Reminders;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class Main{
    
    private static final Uri CAL_URI = CalendarContract.Calendars.CONTENT_URI;
    static final String LOG = "CalendarUtil";
    
     
    public static String createCalendar(CalendarRequest crx)
            throws Exception {
        ContentResolver cr = crx.getContext().getContentResolver();
        final ContentValues cv = getCalendarContentValues(
                crx.getCalendarName(), crx.getAccountName());
        Uri calUri = buildCalUri(crx.getAccountName());
        

        Uri u = cr.insert(calUri, cv);
        String s = u.toString();
        int index = s.indexOf("calendars/");
        int index2 = s.indexOf("?");
        if (index > -1) {
            index += 10;
            String id = s.substring(index, index2);
            Log.i(LOG, "----------- Substring gave this calendar id: " + id);
        }

        Log.i(LOG, "Calendar created: " + u.toString());

        return u.toString();
    }
    private static ContentValues getCalendarContentValues(
            String calendarName, String accountName) {

        final ContentValues cv = new ContentValues();
        cv.put(Calendars.ACCOUNT_NAME, accountName);
        cv.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        cv.put(Calendars.NAME, calendarName);
        cv.put(Calendars.CALENDAR_DISPLAY_NAME, calendarName);
        cv.put(Calendars.CALENDAR_COLOR, 0xEA8561);
        
        cv.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        cv.put(Calendars.OWNER_ACCOUNT, accountName);
        cv.put(Calendars.VISIBLE, 1);
        cv.put(Calendars.SYNC_EVENTS, 1);
        return cv;
    }
    

    private static Uri buildCalUri(String accountName) {
        return CAL_URI
                .buildUpon()
                .appendQueryParameter(
                        CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, accountName)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE,
                        CalendarContract.ACCOUNT_TYPE_LOCAL).build();
    }
}

 
继续阅读
编程十万个怎么办