| 數(shù)據(jù)表 | 主要功能 | 主要字段 |
| ------------- |:-------------:|: -----:|
|calls表 | 存儲(chǔ)通話記錄信息 | number(電話號(hào)碼)、 date(通話日期)区转、name(通話名稱)戒努、geocoded_location(電話號(hào)碼所屬地) |
| groups表 | 存儲(chǔ)手機(jī)MAC地址信息 | title(手機(jī)的mac地址) _id(Group_ID) |
| data表 | 存儲(chǔ)聯(lián)系人信息 | data1(聯(lián)系人號(hào)碼、聯(lián)系人名稱、Group_id)、 raw_contact_id(一個(gè)聯(lián)系人ID) |
| search_index表 | 查詢聯(lián)系人號(hào)碼 | contact_id(其實(shí)跟data表raw_contact_id關(guān)聯(lián)) tokens(聯(lián)系人號(hào)碼) |
主要思路
- 1.聯(lián)系人的數(shù)據(jù)庫(kù)文件的位置:/data/data/com.android.providers.contacts/databases/contacts2.db
- 2.contacts表:保存了所有的手機(jī)測(cè)聯(lián)系人瞪醋,每個(gè)聯(lián)系人占一行,該表保存了聯(lián)系人的
ContactID休弃、聯(lián)系次數(shù)吞歼、最后一次聯(lián)系的時(shí)間、是否含有號(hào)碼塔猾、是否被添加到收藏夾等信息篙骡。 - 3.data表:保存了所有創(chuàng)建過的手機(jī)測(cè)聯(lián)系人的所有信息,每個(gè)字段占一行 ,該表
保存了兩個(gè)ID:MimeTypeID和RawContactID,從而將data表和raw_contacts表聯(lián)系起來糯俗。 - 4.在AndroidManifest.xml文件中配置如下權(quán)限:
<uses-permission android:name="android.permission.READ_CONTACTS"
/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"
/>
1.calls表操作
主要對(duì)通話記錄數(shù)據(jù)表清空和批量插入20條通話記錄信息
public void BatchDeleteCallLog(){
try {
context.getContentResolver().delete(
android.provider.CallLog.Calls.CONTENT_URI, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
批量插入20條通話記錄信息
int size=0;
public void BatchAddCallLog(Vector<CallLog> list) throws RemoteException,
OperationApplicationException {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ContentValues values = new ContentValues();
Log.i(TAG, "name" + list.get(0).getName() + "number"
+ list.get(0).getNum());
Log.i(TAG, "type" + String.valueOf(list.get(0).getRecordType())
+ "date" + String.valueOf(list.get(0).getTime()) + "duration"
+ String.valueOf(list.get(0).getDuration()));
// for (CallLog calllog : list) {
if(list.size()>20){
size=20;
}else{
size=list.size();
}
for (int i = 0; i < size; i++) {
int type = list.get(i).getRecordType();
if (type == 3 || type == 1) {
type = 3;
} else if (type == 2) {
type = 1;
} else if (type == 4) {
type = 2;
}
values.clear();
values.put(android.provider.CallLog.Calls.CACHED_NAME, list.get(i).
getName());
values.put(android.provider.CallLog.Calls.NUMBER, list.get(i)
.getNum());
values.put(android.provider.CallLog.Calls.TYPE,
String.valueOf(type));
values.put(android.provider.CallLog.Calls.DATE,
String.valueOf(list.get(i).getTime()));
values.put(android.provider.CallLog.Calls.DURATION,
String.valueOf(list.get(i).getDuration()));
values.put(android.provider.CallLog.Calls.NEW, "0");
ops.add(ContentProviderOperation
.newInsert(android.provider.CallLog.CONTENT_URI)
.withValues(values).withYieldAllowed(true).build());
}
// }
if (ops != null) {
// 真正添加
ContentProviderResult[] results = context.getContentResolver()
.applyBatch(android.provider.CallLog.AUTHORITY, ops);
Log.i(TAG, "insert success");
}
}
2groups表操作
主要對(duì)Groups表查詢個(gè)數(shù)尿褪、查詢存儲(chǔ)的mac是否存在、插入mac地址得湘、根據(jù)mac地址找到Group_id杖玲、根據(jù)mac地址刪除對(duì)應(yīng)信息、清空groups表淘正。
Groups表查詢個(gè)數(shù)
public int queryMacCountFromSystem(final Context context) {
Cursor cursor = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, null, null, null);
Log.i(TAG, "cursor = "+cursor);
if(cursor == null) {
return 0;
} else {
return cursor.getCount();
}
}
查詢存儲(chǔ)的mac是否存在
public boolean queryMacFromSystem(final Context context, String deviceId) {
String where = ContactsContract.Groups.TITLE + " = ? ";
String[] param = new String[] { deviceId + "" };
Cursor cursor = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, where, param, null);
if(cursor == null) {
return false;
} else {
if(cursor.getCount()>0) {
return true;
}
}
return false;
}
插入mac地址
public boolean insertMacToSystem(Vector<Contacts> contactsList, String deviceId) {
if ((contactsList == null) || (contactsList.size() == 0) || !isDBEnable()) {
return false;
}
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
L.i(TAG, "insert mac message !");
ops.add(ContentProviderOperation
.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.withValue(RawContacts.AGGREGATION_MODE,
RawContacts.AGGREGATION_MODE_DISABLED)
.withYieldAllowed(true).build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Groups.CONTENT_URI)
.withValue(Groups._ID,null)
.withValue(Groups.TITLE, deviceId)
.withYieldAllowed(true).build());
if ((ops != null) && (ops.size() > 0)) {
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Log.i(TAG, "insertContactsToSystem Mac OK!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return false;
}
根據(jù)mac地址找到Group_id
public int queryIndexGroupID(String groupName) {
String where = Groups.TITLE + " = ? ";
String[] param = new String[] { groupName };
try{
Cursor cursor = context.getContentResolver().query(Groups.CONTENT_URI, null, where, param, null);
if ((cursor != null) && cursor.moveToFirst()) {
return cursor.getInt(cursor.getColumnIndex(Groups._ID));
}
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
根據(jù)mac地址刪除對(duì)應(yīng)信息
public boolean delMacFromSystem(final Context context, String deviceId) {
String where = ContactsContract.Groups.TITLE + " = ? ";
String[] param = new String[] { deviceId + "" };
Cursor cursor = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, where, param, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(Groups._ID));
Uri uri = Uri.parse(Groups.CONTENT_URI + "?" + ContactsContract.CALLER_IS_SYNCADAPTER + "=true");
ops.add(ContentProviderOperation.newDelete(ContentUris.withAppendedId(uri, id))
.withYieldAllowed(true).build());
}
try {
if ((ops != null) && (ops.size() > 0)) {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (cursor != null) {
cursor.close();
}
}
return false;
}
3.data表操作
批量插入摆马、批量刪除、根據(jù)Group_id批量插入(其中解決批量插入時(shí)鸿吆,聯(lián)系人數(shù)據(jù)過大導(dǎo)致插入失敗問題)
批量插入
public void batchAddContact(Context context, Vector<Contacts> list, int groupsId) {
Log.i(TAG,"start insert DataBase, Contacts size ==" + list.size()+ "groupsId =" +groupsId);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = 0;
int count = 0;
int startIndex = 0;
String numbers;
int[] types;
for(Contacts contacts : list) {
rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation
.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.withValue(RawContacts.AGGREGATION_MODE,
RawContacts.AGGREGATION_MODE_DISABLED)
.withYieldAllowed(true).build());
//添加姓名
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, contacts.getName())
.withYieldAllowed(true).build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, contacts.getNum())
.withValue(Phone.TYPE, Phone.TYPE_MOBILE)
.withYieldAllowed(true).build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE)
.withValue(GroupMembership.GROUP_ROW_ID,
String.valueOf(groupsId))
.withYieldAllowed(true).build());
//兩百條同步一次
count++;
Log.i(TAG, "count ="+count);
if(count == 200) {
if(ops !=null) {
//真正添加
try{
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Log.e(TAG, "applyBatch contacts success >200!");
} catch (RemoteException e) {
e.printStackTrace();
Log.e(TAG, "applyBatch error!");
} catch (OperationApplicationException e) {
e.printStackTrace();
Log.e(TAG,"applyBatch error!");
}
}
count = 0;
ops.clear();
Log.i(TAG, "add 200 size!!!!");
}
}
if(ops != null) {
//真正添加
try{
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Log.e(TAG, "applyBatch contacts success!");
} catch (RemoteException e) {
e.printStackTrace();
Log.e(TAG, "applyBatch error!");
} catch (OperationApplicationException e) {
e.printStackTrace();
Log.e(TAG,"applyBatch error!");
}
Log.i(TAG, "add stop");
}
Log.i(TAG, "add success ok !!!!!");
}
批量刪除
public boolean delContactsFromSystem(final Context context, int idd) {
String where = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = ? ";
String[] param = new String[] { idd + "" };
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, param, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
L.i(TAG, "cursor"+cursor);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
L.i(TAG, "strid == "+id);
ops.add(ContentProviderOperation.newDelete(ContentUris.withAppendedId(RawContacts.CONTENT_URI, id))
.withYieldAllowed(true).build());
}
try {
if ((ops != null) && (ops.size() > 0)) {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Log.i(TAG, "delContactsFromSystem OK!");
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (cursor != null) {
cursor.close();
}
}
return false;
}