对于系统手机的联系人、短信、通话记录的一些列的方法,着实需要总结下了
我公司最近在做跟这相关的项目,这个博客后续会完善这3个模块的工具类方法
1、查询contacts表获取contactId, 通过contact_id去获取data表中相应的数据
/** * 通过contactId从data表中获取联系人信息 Uri uri = * Uri.parse("content://com.android.contacts/contacts/#/data"); * 查询contacts表获取contactId, 通过contact_id去获取data表中相应的数据 */ public static void getContactInfoFromDataByContactId(Context mContext) { // 访问contacts表 Uri uri = Uri.parse("content://com.android.contacts/contacts"); ContentResolver resolver = mContext.getContentResolver(); // 获得contact_id属性 Cursor cursor = resolver.query(uri, new String[] { Data._ID }, null, null, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { int contactId = cursor.getInt(0); // 通过contact_id,去获取data表中对应的值 uri = Uri.parse("content://com.android.contacts/contacts/" + contactId + "/data"); Cursor cursor2 = resolver.query(uri, new String[] { Data.DATA1, Data.DATA15, Data.MIMETYPE }, null, null, null); // data1存储各个记录的总数据,mimetype存放记录的类型,如电话、email等 while (cursor2.moveToNext()) { String data = cursor2.getString(cursor2 .getColumnIndex("data1")); byte[] photo = cursor2.getBlob(cursor2 .getColumnIndex("data15")); if (cursor2.getString(cursor2.getColumnIndex("mimetype")) .equals("vnd.android.cursor.item/name")) { // 如果是名字 Log.i("TAG", "name" + data); } else if (cursor2.getString( cursor2.getColumnIndex("mimetype")).equals( "vnd.android.cursor.item/phone_v2")) { // 如果是电话 Log.i("TAG", "phone" + data); } else if (cursor2.getString( cursor2.getColumnIndex("mimetype")).equals( "vnd.android.cursor.item/photo")) { // 如果是电话 Log.i("TAG", "photo" + photo.length); } } Log.i("TAG", "--------------------"); } } }
2、获取联系人头像的3种方式
/************************************************************* * 1、获取联系人头像 * 通过contactId获取联系人头像 查询contacts表获取contactId, * 通过contact_id去获取data表中相应的photo数据 */ public static Bitmap getContactPhotoFromDataByContactId(Context mContext, String contactId) { Bitmap bitmap = null; // 访问contacts表 Uri uri = Uri.parse("content://com.android.contacts/contacts/" + contactId + "/data"); Cursor cursor = mContext.getContentResolver().query(uri, new String[] { Data.DATA15, Data.MIMETYPE }, null, null, null); // data1存储各个记录的总数据,mimetype存放记录的类型,如电话、email等 if (cursor != null && cursor.getCount() > 0) { // 通过contact_id,去获取data表中对应的值 while (cursor.moveToNext()) { byte[] photoByte = cursor.getBlob(cursor .getColumnIndex("data15")); if (cursor.getString(cursor.getColumnIndex("mimetype")).equals( "vnd.android.cursor.item/photo")) { // 如果是电话 Log.i("TAG", "photo" + photoByte.length); bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length); } } } return bitmap; } /**2、获取联系人头像 * 通过contactId获取联系人头像 * openContactPhotoInputStream进行读取操作 */ public static Bitmap getContactPhotoByOpenContactPhotoInputStream( Context mContext, String contactId) { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId)); InputStream input = ContactsContract.Contacts .openContactPhotoInputStream(mContext.getContentResolver(), uri, false); if (input == null) { return null; } return BitmapFactory.decodeStream(input); } /**3、获取联系人头像 * 通过电话号码获取头像 * 通过电话号码获取contactId * 如果2个contactId,都有相同的电话号码,这里的代码默认取第一个联系人的头像 * openContactPhotoInputStream进行读取操作 */ public static Bitmap getContactPhotoDataPhoneFilter(Context mContext,String PhoneNumber){ Bitmap bitmap = null; // 获得Uri Uri uriNumber2Contacts = Uri.parse("content://com.android.contacts/" + "data/phones/filter/" + PhoneNumber); // 查询Uri,返回数据集 Cursor cursorCantacts = mContext.getContentResolver().query( uriNumber2Contacts, null, null, null, null); if (cursorCantacts!=null && cursorCantacts.getCount()>0) { if ( cursorCantacts.moveToFirst()) { Long contactID = cursorCantacts.getLong(cursorCantacts.getColumnIndex("contact_id")); Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactID); InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(mContext.getContentResolver(), uri); return (bitmap = BitmapFactory.decodeStream(input)); } } return bitmap; }
3、从contacts表中获取contactId、photoId、rawContactId值
public static void getSingleColumnsFromContactsTable(Context mContext,String contactId) { // 访问contacts表 Uri uri = Uri.parse("content://com.android.contacts/contacts"); ContentResolver resolver = mContext.getContentResolver(); // 从contacts表中获取contactId、photoId、rawContactId Cursor cursor = resolver.query(uri, new String[] { Data._ID, CommonDataKinds.Photo.PHOTO_ID,CommonDataKinds.Phone.NAME_RAW_CONTACT_ID}, "_id=?", new String[]{contactId}, null); if (cursor != null && cursor.getCount() > 0) { if (cursor.moveToFirst()) { int cursorContactId = cursor.getInt(0); int photoId = cursor.getInt(cursor .getColumnIndex(CommonDataKinds.Phone.PHOTO_ID)); int rawContactId = cursor.getInt(cursor .getColumnIndex(CommonDataKinds.Phone.NAME_RAW_CONTACT_ID)); Log.i("TAG", "contactId:" + cursorContactId); Log.i("TAG", "photoId:" + photoId); Log.i("TAG", "rawContactId:" + rawContactId); } } }
4、通过contact_id获取photo_id
/** * 通过contactId获取头像photo_id * Uri photoIdByContactId = * Uri.parse("content://com.android.contacts/contacts/"+1+"/photo"); */ public static void getRawContactIdByContactId(Context mContext) { // 访问contacts表 Uri uri = Uri.parse("content://com.android.contacts/contacts"); ContentResolver resolver = mContext.getContentResolver(); // 从contacts表中获取contactId Cursor cursor = resolver.query(uri, new String[] { Data._ID }, null, null, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { int contactId = cursor.getInt(0); Cursor query = resolver .query(Uri .parse("content://com.android.contacts/contacts/" + contactId + "/photo"), null, null, null, null); if (query != null && query.getCount() > 0 && query.moveToFirst()) { int photoId = query.getInt(query .getColumnIndex(Phone.PHOTO_ID)); Log.i("TAG", "contactId:" + contactId+ ",photoId:" + photoId); } } } }
5、查询raw_contacts表中的一些需要信息,例如displayName、sortKey、rawContactId、contactId
/** * contactId为空的就是被删掉的联系人 * 查询raw_contacts表,去获取联系人的信息 */ public static void getColumnsFromRawContactsTable(Context mContext) { // 访问contacts表 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentResolver resolver = mContext.getContentResolver(); String[] projection = { Data._ID, CommonDataKinds.Phone.CONTACT_ID, CommonDataKinds.Phone.DISPLAY_NAME, CommonDataKinds.Phone.SORT_KEY_PRIMARY,"phonebook_label" };//sort_key Cursor cursor = resolver.query(uri, projection, null, null, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { int rawContactId = cursor.getInt(0); int contactId = cursor.getInt(1); String displayName = cursor.getString(2); String sortKey = cursor.getString(3); String phonebook_label = cursor.getString(4); if (contactId!=0) { Log.i("TAG", "rawContactId:" + rawContactId); Log.i("TAG", "contactId:" + contactId); Log.i("TAG", "displayName:" + displayName); Log.i("TAG", "sortKey:" + sortKey); Log.i("TAG", "sortKey:" + phonebook_label); Log.i("TAG", "--------------"); } } } }
6、根据电话号码获取联系人姓名
/** * 根据电话号码获取联系人姓名 * 如果2个联系人有同样的号码,默认获取第一个 */ public static void getContactNameByPhonNum(Context mContext, String phoneNum) { ContentResolver resolver = mContext.getContentResolver(); Uri uri = Uri .parse("content://com.android.contacts/data/phones/filter/" + phoneNum); Cursor c = resolver.query(uri, new String[] { "display_name" }, null, null, null); if (c != null && c.getCount() > 0 && c.moveToFirst()) { Log.i("TAG", "phoneNum:" + c.getString(0)); } }
7、添加联系人
/** * 添加联系人 * @param mContext */ public static boolean insertContact(Context mContext,String given_name, String mobile_number, String work_email) { ContentValues values = new ContentValues(); // 下面的操作会根据RawContacts表中已有的rawContactId使用情况自动生成新联系人的rawContactId Uri rawContactUri = mContext.getContentResolver().insert( RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); // 向data表插入姓名数据 if (given_name != "") { values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); values.put(StructuredName.GIVEN_NAME, given_name); mContext.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); } // 向data表插入电话数据 if (mobile_number != "") { values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, mobile_number); values.put(Phone.TYPE, Phone.TYPE_MOBILE); mContext.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); } // 向data表插入Email数据 if (work_email != "") { values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); values.put(Email.DATA, work_email); values.put(Email.TYPE, Email.TYPE_WORK); mContext.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); } values.clear(); // 向data表插入头像数据 Bitmap sourceBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher); final ByteArrayOutputStream os = new ByteArrayOutputStream(); // 将Bitmap压缩成PNG编码,质量为100%存储 sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, os); byte[] avatar = os.toByteArray(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE); values.put(Photo.PHOTO, avatar); mContext.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); return true; }
8、根据字母查询联系人、根据电话号码模糊查询
/** * 根据名字中的某一个字进行模糊查询 * matcher.addURI(ContactsContract.AUTHORITY, "data/phones", PHONES); * @param key */ public static void getFuzzyQueryByName(Context mContext, String key) { StringBuilder sb = new StringBuilder(); ContentResolver cr = mContext.getContentResolver(); //display_name、data1 String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; Cursor cursor = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.Contacts.DISPLAY_NAME + " like " + "'%" + key + "%'", null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String number = cursor .getString(cursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); sb.append(name + " (").append(number + ")").append("\r\n"); } cursor.close(); if (!sb.toString().isEmpty()) { Log.d("TAG", "查询联系人:\r\n" + sb.toString()); } } /** * 根据电话号码进行模糊查询 * */ public static void getFuzzyByPhoneNum(Context mContext, String key) { String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; Cursor cursor = mContext.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Phone.NUMBER + " like " + "'%" + key + "%'", null, null); if (cursor == null) { return; } for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); String name = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String number = cursor .getString(cursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Log.i("TAG", "根据号码查联系人 name: " + name + "----" + " number:" + number + " \n"); // 这里提示 } }
9、获取联系人,通过获取contactId,继而获取raw_contactId,继而获取联系人数据
/** * 读取联系人 1、先读取contacts表,获取ContactsID; * * 2、再在raw_contacts表中根据ContactsID获取RawContactsID; * * 3、然后就可以在data表中根据RawContactsID获取该联系人的各数据了。 */ public static void queryContactsByContactIdAndRawContactId(Context mContext) { // 获取用来操作数据的类的对象,对联系人的基本操作都是使用这个对象 ContentResolver cr = mContext.getContentResolver(); // 查询contacts表的所有记录 Cursor contactCursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (contactCursor.getCount() > 0) { // 游标初始指向查询结果的第一条记录的上方,执行moveToNext函数会判断 // 下一条记录是否存在,如果存在,指向下一条记录。否则,返回false。 while (contactCursor.moveToNext()) { String rawContactId = ""; String id = contactCursor.getString(contactCursor .getColumnIndex(ContactsContract.Contacts._ID)); Log.v("TAG", "id:"+id); Cursor rawContactCur = cr.query(RawContacts.CONTENT_URI, null, ContactsContract.CommonDataKinds.Contactables.CONTACT_ID + "=?", new String[] { id }, null); if (rawContactCur.moveToFirst()) { rawContactId = rawContactCur.getString(rawContactCur .getColumnIndex(RawContacts._ID)); Log.v("TAG", "rawContactId:"+rawContactId); } rawContactCur.close(); // 读取号码 if (Integer .parseInt(contactCursor.getString(contactCursor .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { // 根据查询RAW_CONTACT_ID查询该联系人的号码 Cursor phoneCur = cr .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID + "=?", new String[] { rawContactId }, null); // 上面的ContactsContract.CommonDataKinds.Phone.CONTENT_URI // 可以用下面的phoneUri代替 // phoneUri=Uri.parse("content://com.android.contacts/data/phones"); // 一个联系人可能有多个号码,需要遍历 while (phoneCur.moveToNext()) { String number = phoneCur .getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Log.v("TAG", "number:"+number); } phoneCur.close(); } } contactCursor.close(); } }
10、删除、更新联系人
// 删除联系人 public static void deleteContact(Context mContext, long rawContactId) { mContext.getContentResolver().delete( ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), null, null); } /** * 更新联系人 * @param mContext * @param rawContactId * * 代码不严谨, * 因为raw_contacts表中的contact_id列为0时候,说明这个联系人被删除了, * 应该先通过raw_contactid 判断contact_id是否为0,如果不为0,在进行更新操作 * 可以直接在raw_contacts表中,也可以在contacts表中去判断都可 */ public static void updataCotact(Context mContext, long rawContactId) { ContentValues values = new ContentValues(); values.put(Phone.NUMBER, "13800138000"); values.put(Phone.TYPE, Phone.TYPE_MOBILE); String where = ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?"; String[] selectionArgs = new String[] { String.valueOf(rawContactId), Phone.CONTENT_ITEM_TYPE }; mContext.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, where, selectionArgs); }
时间: 2024-11-10 14:20:31