android获取手机中的短信和,android获取手机通讯录联系人和号码
16lz
2021-12-04
阅读更多
// 获得所有的联系人Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC");// 循环遍历if (cur.moveToFirst()) {int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);do {// 获得联系人的ID号String contactId = cur.getString(idColumn);// 获得联系人姓名String disPlayName = cur.getString(displayNameColumn);// 查看该联系人有多少个电话号码。如果没有这返回值为0int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));Log.i("username", disPlayName);sb.append(disPlayName).append(":"); if (phoneCount > 0) {// 获得联系人的电话号码Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId, null, null);if (phones.moveToFirst()) {do {// 遍历所有的电话号码String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));String phoneType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));Log.i("phoneNumber", phoneNumber);Log.i("phoneType", phoneType);sb.append(phoneNumber).append(","); } while (phones.moveToNext());}}// 获取该联系人邮箱Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId, null, null);if (emails.moveToFirst()) {do {// 遍历所有的电话号码String emailType = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));String emailValue = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));Log.i("emailType", emailType);Log.i("emailValue", emailValue);} while (emails.moveToNext());}// 获取该联系人IMCursor IMs = getContentResolver().query(Data.CONTENT_URI,new String[] { Data._ID, Im.PROTOCOL, Im.DATA },Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"+ Im.CONTENT_ITEM_TYPE + "'",new String[] { contactId }, null);if (IMs.moveToFirst()) {do {String protocol = IMs.getString(IMs.getColumnIndex(Im.PROTOCOL));String date = IMs.getString(IMs.getColumnIndex(Im.DATA));Log.i("protocol", protocol);Log.i("date", date);} while (IMs.moveToNext());}// 获取该联系人地址Cursor address = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId, null, null);if (address.moveToFirst()) {do {// 遍历所有的地址String street = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));String city = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));String region = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));String postCode = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));String formatAddress = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));Log.i("street", street);Log.i("city", city);Log.i("region", region);Log.i("postCode", postCode);Log.i("formatAddress", formatAddress);} while (address.moveToNext());}// 获取该联系人组织Cursor organizations = getContentResolver().query(Data.CONTENT_URI,new String[] { Data._ID, Organization.COMPANY,Organization.TITLE },Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"+ Organization.CONTENT_ITEM_TYPE + "'",new String[] { contactId }, null);if (organizations.moveToFirst()) {do {String company = organizations.getString(organizations.getColumnIndex(Organization.COMPANY));String title = organizations.getString(organizations.getColumnIndex(Organization.TITLE));Log.i("company", company);Log.i("title", title);} while (organizations.moveToNext());}// 获取备注信息Cursor notes = getContentResolver().query(Data.CONTENT_URI,new String[] { Data._ID, Note.NOTE },Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"+ Note.CONTENT_ITEM_TYPE + "'",new String[] { contactId }, null);if (notes.moveToFirst()) {do {String noteinfo = notes.getString(notes.getColumnIndex(Note.NOTE));Log.i("noteinfo", noteinfo);} while (notes.moveToNext());}// 获取nickname信息Cursor nicknames = getContentResolver().query(Data.CONTENT_URI,new String[] { Data._ID, Nickname.NAME },Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"+ Nickname.CONTENT_ITEM_TYPE + "'",new String[] { contactId }, null);if (nicknames.moveToFirst()) {do {String nickname_ = nicknames.getString(nicknames.getColumnIndex(Nickname.NAME));Log.i("nickname_", nickname_);} while (nicknames.moveToNext());}} while (cur.moveToNext());}
源码下载地址 : http://www.itbbu.com/?p=400
更多相关文章
- android点击状态分析
- Android系统调用
- Android开发个人小记
- Android获取屏幕宽度与高度
- android google地图定位开发,且可以自由移动位置重新获取定位,地址
- Android(安卓)为联系人添加图片
- Android(安卓)各种音量的获取和设置
- Android对话框里面的输入值获取不到,空指针异常
- 初涉Android蓝牙开发 收藏以备后用