/**
* 获取所有联系人
*
* @param context
* @return
*/
public static List<ContactBean> getContacts(Context context) {
List<ContactBean> cb = new ArrayList<>();
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
ContactBean bean = new ContactBean();
int columnIndexDISPLAY_NAME = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int _ID = cursor.getInt(columnIndex_ID);
String displayName = cursor.getString(columnIndexDISPLAY_NAME);
bean.set_ID(_ID);
bean.setName(displayName);
List<String> phonesList = new ArrayList<>();
// 获取联系人手机号码
Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + _ID, null, null);
if (phoneCursor != null && phoneCursor.moveToFirst()) {
do {
String phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phonesList.add(phone);
} while (phoneCursor.moveToNext());
phoneCursor.close();
}
bean.setPhone(phonesList);
String uriPhoto = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
bean.setUriPhoto(uriPhoto);
cb.add(bean);
} while (cursor.moveToNext());
cursor.close();
}
return cb;
}
/**
* 根据key进行模糊查询
* http://www.cnblogs.com/snake-hand/archive/2013/06/05/3119778.html
*
* @param key
*/
@SuppressLint("InlinedApi")
public static List<ContactBean> getContactsFuzzyQueryByKey(Context context, String key) {
List<ContactBean> cb = new ArrayList<>();
ContentResolver cr = context.getContentResolver();
String[] projection = { ContactsContract.Contacts._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.PHOTO_URI, ContactsContract.CommonDataKinds.Phone.NUMBER };
StringBuilder selection = new StringBuilder();
selection.append(ContactsContract.Contacts.DISPLAY_NAME);
selection.append(" LIKE '%" + key + "%' ");
selection.append(" OR ");
selection.append(ContactsContract.CommonDataKinds.Phone.NUMBER);
selection.append(" LIKE '%" + key + "%' ");
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, selection.toString(), null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
ContactBean bean = new ContactBean();
long _id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String uriPhoto = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
bean.set_ID(_id);
bean.setName(name);
// bean.setPhone(number);
bean.setUriPhoto(uriPhoto);
cb.add(bean);
} while (cursor.moveToNext());
cursor.close();
}
return cb;
}
/**
* 通过电话查询联系人
*
* @param context
* @param phoneNumber
* @return
*/
public static String lookupNameByPhoneNumber(Context context, String phoneNumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = context.getContentResolver().query(uri, new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
String name = null;
if (cursor == null) {
name = null;
}
try {
if (cursor.moveToFirst()) {
name = cursor.getString(0);
}
} catch (Exception e) {
name = "";
} finally {
cursor.close();
}
return name;
}
/**
* Contacts点击跳转到通讯录界面
*
* @param context
*/
public static void selectContacts(Activity context, int requestCode) {
Uri uri = Uri.parse("content://contacts/people");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
context.startActivityForResult(intent, requestCode);
}
public static void selectContactsOnActivityResult(Context context, int requestCode, int resultCode, Intent data) {
if (data == null) {
return;
}
Uri uri = data.getData();
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
return;
}
if (cursor.moveToFirst()) {
int columnIndexDISPLAY_NAME = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String displayName = cursor.getString(columnIndexDISPLAY_NAME);
// https://my.oschina.net/moziqi/blog/365727
cursor.close();
}
}
public static List<MsgBean> getSmsFuzzyQueryByKey(Context context, String key) {
List<MsgBean> msgs = new ArrayList<>();
ContentResolver cr = context.getContentResolver();
String[] projection = { Sms.SEEN };
StringBuilder selection = new StringBuilder();
selection.append(Sms.BODY);
selection.append(" LIKE '%" + key + "%' ");
Cursor cursor = cr.query(Sms.CONTENT_URI, null, selection.toString(), null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
MsgBean bean = new MsgBean();
long _ID = cursor.getLong(cursor.getColumnIndex(Sms._ID));
String body = cursor.getString(cursor.getColumnIndex(Sms.BODY));
int type = cursor.getInt(cursor.getColumnIndex(Sms.TYPE));
int thread_id = cursor.getInt(cursor.getColumnIndex(Sms.THREAD_ID));
int person = cursor.getInt(cursor.getColumnIndex(Sms.PERSON));
String address = cursor.getString(cursor.getColumnIndex(Sms.ADDRESS));
long date = cursor.getLong(cursor.getColumnIndex(Sms.DATE));
long date_sent = cursor.getLong(cursor.getColumnIndex(Sms.DATE_SENT));
bean.set_ID(_ID);
bean.setContent(body);
bean.setType(type);
bean.setPersonID(person);
bean.setAddress(address);
bean.setDate(date);
bean.setDate_sent(date_sent);
bean.setThread_id(thread_id);
//通过地址查询名字
//http://stackoverflow.com/questions/35112734/how-to-read-contact-name-from-inbox-messages
final String[] phoneProjection = new String[] { ContactsContract.PhoneLookup.PHOTO_URI, ContactsContract.Data.DISPLAY_NAME };
Cursor contactCursor = null;
try {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
contactCursor = cr.query(uri, phoneProjection, null, null, null);
if (contactCursor != null && contactCursor.moveToFirst()) {
String name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
String uriPhoto = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
bean.setUriPhoto(uriPhoto);// 设置头像
bean.setAddress(name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (contactCursor != null) {
contactCursor.close();
}
}
msgs.add(bean);
} while (cursor.moveToNext());
cursor.close();
}
return msgs;
}
holderContact.contentRelativeLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 打开联系人
//http://stackoverflow.com/questions/4275167/how-to-open-a-contact-card-in-android-by-id
try {
if (contactBean.get_ID() > 0) {
Intent defineIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactBean.get_ID()));
defineIntent.setData(uri);
defineIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(defineIntent);
}
} catch (Exception e) {
LogDebug.e(context, e.getMessage());
}
}
});
holderMessage.contentRelativeLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// http://stackoverflow.com/questions/1412221/how-to-open-up-a-specific-sms-in-android
Intent defineIntent = new Intent(Intent.ACTION_VIEW);
defineIntent.setData(Uri.parse("content://mms-sms/conversations/" + msgBean.getThread_id()));
defineIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(defineIntent);
} catch (Exception e) {
}
}
});