一、群组数据库contact2.db中的group
1.数据库位置
/data/data/com.android.providers.contacts/databases/contact2.db
2.Group表的内容截图
3.各代表的意思
_id 行id
package_id
account_name 账户名字
account_type 帐户的类型
sourceid
version 版本;当列或相关数据修改时,将会自动修改
dirty 版本发生改变的标记
title 这组显示的标题
title_res
notes 这组的注释
system_id 如果它是一个系统组,这个组的ID,即对同步适配器具有特殊意义的一组,否则返回null。
deleted 删除标记;0 or 1;1 表示标记为被删除。
group_visible 群组是否在UI中 是否可见;是(1),否(0)
should_sync 这组是否应该同步,是(1),否(0)
sync1到sync4
Groups.TITLE ==标题
Groups.ACCOUNT_NAME ==账户名
Groups.ACCOUNT_TYPE ==账户类型
Groups.DELETED== 是否被删除
Groups.GROUP_IS_READ_ONLY==是否只能读取 (智能群组为1)
注:群组中有智能群组和手机群组,其中智能群组仅能读取,Groups.GROUP_IS_READ_ONLY==1
二、创建群组
1.判断群组名是否已经存在
public static boolean checkGroupNameExist(Context context, String groupName,
String accountName, String accountType) {
boolean nameExists = false;
if (TextUtils.isEmpty(groupName)) {
showToastInt(R.string.name_needed);
return false;
}
Cursor cursor = context.getContentResolver().query(
Groups.CONTENT_SUMMARY_URI , new String[] { Groups._ID },
Groups.TITLE + "=? AND " + Groups.ACCOUNT_NAME + " =? AND " +
Groups.ACCOUNT_TYPE+ "=? AND " + Groups.DELETED + " = 0 AND " +
Groups.GROUP_IS_READ_ONLY + " = 0 ",
new String[] { groupName, accountName, accountType }, null);
if (cursor != null) {
if (cursor.getCount() > 0) {
nameExists = true;
}
cursor.close();
}
// If group name exists, make a toast and return false.
if (nameExists) {
showToastInt(R.string.group_name_exists);
return false;
} else {
return true;
}
}
上面的代码重点为以下的部分:
Cursor cursor = context.getContentResolver().query(
Groups.CONTENT_SUMMARY_URI , new String[] { Groups._ID },
Groups.TITLE + "=? AND " + Groups.ACCOUNT_NAME + " =? AND " +
Groups.ACCOUNT_TYPE+ "=? AND " + Groups.DELETED + " = 0 AND " +
Groups.GROUP_IS_READ_ONLY + " = 0 ",
new String[] { groupName, accountName, accountType }, null);
首先query方法的参数:
public final Cursor query (Uri uri, String[] projection,String selection,
String[]selectionArgs, String sortOrder)
uri :内容的地址,
projection :参数为查询要返回的列(Column),
selection :查询where字句
selectionArgs : 查询条件属性值
sortOrder :结果排序规则
2.创建群组
ContactSaveService中有如下代码可供参考:
private void createGroup(Intent intent) {
//获取group的相关属性
String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE);
String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME);
String dataSet = intent.getStringExtra(EXTRA_DATA_SET);
String label = intent.getStringExtra(EXTRA_GROUP_LABEL);
final long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD);
//判断群组是否已经存在 @{
Log.d(TAG, "[createGroupToIcc]groupName:" + label + " ,accountName:" + accountName
+ ",AccountType:" + accountType);
if (!ContactSaveServiceEx.checkGroupNameExist(this, label, accountName, accountType,
true)) {
Log.d(TAG, "[createGroupToIcc] Group Name exist!");
Intent callbackIntent = intent
.getParcelableExtra(ContactSaveService.EXTRA_CALLBACK_INTENT);
callbackIntent.putExtra(ContactEditorFragment.SAVE_MODE_EXTRA_KEY, SaveMode.RELOAD);
deliverCallback(callbackIntent);
return;
}
///@}
ContentValues values = new ContentValues();
values.put(Groups.ACCOUNT_TYPE, accountType);
values.put(Groups.ACCOUNT_NAME, accountName);
values.put(Groups.DATA_SET, dataSet);
values.put(Groups.TITLE, label);
final ContentResolver resolver = getContentResolver();
// 创建群组
final Uri groupUri = resolver.insert(Groups.CONTENT_URI, values);
// If there's no URI, then the insertion failed. Abort early because group members can't be
// added if the group doesn't exist
if (groupUri == null) {
Log.e(TAG, "Couldn't create group with label " + label);
return;
}
addMembersToGroup(resolver, rawContactsToAdd, ContentUris.parseId(groupUri));
// TODO: Move this into the contact editor where it belongs. This needs to be integrated
// with the way other intent extras that are passed to the {@link ContactEditorActivity}.
values.clear();
values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);
values.put(GroupMembership.GROUP_ROW_ID, ContentUris.parseId(groupUri));
Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT);
callbackIntent.setData(groupUri);
/// M: fix ALPS00784408 @{
long rawContactId = intent.getLongExtra(EXTRA_RAW_CONTACTS_ID, -1);
callbackIntent.putExtra(EXTRA_RAW_CONTACTS_ID, rawContactId);
/// @}
// TODO: This can be taken out when the above TODO is addressed
callbackIntent.putExtra(ContactsContract.Intents.Insert.DATA, Lists.newArrayList(values));
deliverCallback(callbackIntent);
}
三、其他
GroupCreationDialogFragment 的checkName方法中发现了下面的代码:
// "create new group",show "the group name has exist",the keyboard does not disappear
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
从上面的代码中注释中,可以知道,上面的代码,就是弹出吐司的时候,键盘不隐藏。
数据库参考文章:https://www.2cto.com/kf/201406/309356.html