public class GetContactsInfo {
List<ContactsInfo> localList;
List<ContactsInfo> SIMList;
Context context;
ContactsInfo contactsInfo;
ContactsInfo SIMContactsInfo;
public GetContactsInfo(Context context) {
localList = new ArrayList<ContactsInfo>();
SIMList = new ArrayList<ContactsInfo>();
this.context = context;
}
// ----------------得到本地联系人信息-------------------------------------
public List<ContactsInfo> getLocalContactsInfos() {
ContentResolver cr = context.getContentResolver();
String str[] = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER,
Phone.PHOTO_ID };
Cursor cur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, str, null,
null, null);
if (cur != null) {
while (cur.moveToNext()) {
contactsInfo = new ContactsInfo();
contactsInfo.setContactsPhone(cur.getString(cur
.getColumnIndex(Phone.NUMBER)));// 得到手机号码
contactsInfo.setContactsName(cur.getString(cur
.getColumnIndex(Phone.DISPLAY_NAME)));
// contactsInfo.setContactsPhotoId(cur.getLong(cur.getColumnIndex(Phone.PHOTO_ID)));
long contactid = cur.getLong(cur
.getColumnIndex(Phone.CONTACT_ID));
long photoid = cur.getLong(cur.getColumnIndex(Phone.PHOTO_ID));
// 如果photoid 大于0 表示联系人有头像 ,如果没有给此人设置头像则给他一个默认的
if (photoid > 0) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactid);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(cr, uri);
contactsInfo.setBitmap(BitmapFactory.decodeStream(input));
} else {
contactsInfo.setBitmap(BitmapFactory.decodeResource(
context.getResources(), R.drawable.ic_launcher));
}
System.out.println("---------联系人电话--"
+ contactsInfo.getContactsPhone());
localList.add(contactsInfo);
}
}
cur.close();
return localList;
}
public List<ContactsInfo> getSIMContactsInfos() {
TelephonyManager mTelephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
System.out.println("---------SIM--------");
ContentResolver cr = context.getContentResolver();
final String SIM_URI_ADN = "content://icc/adn";// SIM卡
Uri uri = Uri.parse(SIM_URI_ADN);
Cursor cursor = cr.query(uri, null, null, null, null);
while (cursor.moveToFirst()) {
SIMContactsInfo = new ContactsInfo();
SIMContactsInfo.setContactsName(cursor.getString(cursor
.getColumnIndex("name")));
SIMContactsInfo
.setContactsPhone(cursor.getString(cursor
.getColumnIndex("number")));
SIMContactsInfo
.setBitmap(BitmapFactory.decodeResource(
context.getResources(),
R.drawable.ic_launcher));
SIMList.add(SIMContactsInfo);
}
cursor.close();
return SIMList;
}
}
android获取联系人所有内容
最新推荐文章于 2021-05-27 16:24:46 发布