android获取通讯记录,sim卡和手机

    //获取手机和手机卡通讯录
public static String getContacts()
{
String szContacts = ""; szContacts=getPhoneContacts()+getSIMContacts(); return szContacts;
}
 // 获取手机联系人
public static String getPhoneContacts()
{
String szpPhoneContacts = "";
ContentResolver resolver = s_ctx.getContentResolver();
StringBuffer bufs = new StringBuffer(); // 获取手机联系人
Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PHONES_PROJECTION, null, null, null); JSONObject jsobjs = new JSONObject();
// 不为空
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) { // 得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue; // 得到联系人名称
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME); try
{
jsobjs.put("联系人名称",contactName);
jsobjs.put("电话号码",phoneNumber);
bufs.append(jsobjs); } catch (JSONException e)
{
e.printStackTrace();
} } phoneCursor.close(); }
szpPhoneContacts = bufs.toString();
return szpPhoneContacts;
} /** 得到手机SIM卡联系人人信息 **/
public static String getSIMContacts()
{
StringBuffer bufs = new StringBuffer();
String simconteacts = "";
ContentResolver resolver = s_ctx.getContentResolver();
// 获取Sims卡联系人
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
null); if (phoneCursor != null) {
while (phoneCursor.moveToNext()) { // 得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME); JSONObject jsobj = new JSONObject();
try
{
jsobj.put("联系人名称",contactName);
jsobj.put("电话号码",phoneNumber);
bufs.append(jsobj);
} catch (JSONException e)
{
e.printStackTrace();
} } phoneCursor.close();
} //sim卡中记录
simconteacts = bufs.toString();
return simconteacts;
}
上一篇:【原】iOS学习之第三方-AFNetworking1.3.0


下一篇:[剑指Offer] 50.数组中重复的数字