Android Calendar添加本地账户

来源:互联网 发布:3g模型 淘宝 编辑:程序博客网 时间:2024/04/30 13:56

在Android原生代码中,日历App如要添加活动,需要先添加账户,不方便用户的使用。反编译某某系统的CalendarProvider.apk,从中提取了添加本地账户的代码,在此共享。

主要修改了/packages/providers/CalendarProvider/下的CalendarDatabaseHelper.java文件,其中包括了日历数据库的创建等操作。

// 系统创建日历数据库表结构的函数private void createCalendarsTable(SQLiteDatabase db) {    db.execSQL(/* 创建 Tables.CALENDARS 表 */);    // 添加本地日历账户    insertLocalAccount(db);/* 创建表成功后,添加本地账户到数据库 */    // 创建活动被删除时的触发器    // Trigger to remove a calendar's events when we delete the calendar    db.execSQL("CREATE TRIGGER calendar_cleanup DELETE ON " + Tables.CALENDARS +     " " + "BEGIN " + CALENDAR_CLEANUP_TRIGGER_SQL + "END");}private void insertLocalAccount(SQLiteDatabase db) {    // 添加本地日历账户    insertAccount(db, "account_name_local", "LOCAL",            mContext.getResources().getString(R.string.calendar_displayname_local), 700,            mContext.getResources().getString(R.string.owner_account_local),            mContext.getResources().getColor(R.color.calendar_local_color), 5);    // 添加生日提醒账户 这个需要联系人App作同步修改,不需要的可以不加入    insertAccount(db, "account_name_local", "LOCAL",            mContext.getResources().getString(R.string.calendar_displayname_birthday), 300,            mContext.getResources().getString(R.string.owner_account_local),            mContext.getResources().getColor(R.color.calendar_local_birthday), 0);}private void insertAccount(SQLiteDatabase db, String accountName, String accountType, String calendarDisplayName, int calendarAccessLevel, String ownerAccount, int calendarColor, int maxReminders) {     ContentValues localContentValues = new ContentValues();     localContentValues.put("account_name", accountName);// 账户名称     localContentValues.put("account_type", accountType);// 账户类型     localContentValues.put("calendar_displayName", calendarDisplayName);// 显示的账户名称     localContentValues.put("calendar_access_level", Integer.valueOf(calendarAccessLevel));// 访问级别     localContentValues.put("ownerAccount", ownerAccount);// 账户拥有者     localContentValues.put("calendar_color", Integer.valueOf(calendarColor));// 账户区别颜色     localContentValues.put("sync_events", Integer.valueOf(1));// 是否同步     localContentValues.put("maxReminders", Integer.valueOf(maxReminders));// 最多设置的提醒器数量     db.insert("Calendars", null, localContentValues);}

关于谷歌的账户和同步,有很多需要学习的,与其费劲的自己写日历、联系人的同步,不如学习和使用谷歌提供的账户和SyncAdapter更方便。

0 0