1 给程序添加访问手机联系人信息的权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2
获取手机和 sim卡的方式 除了 uri地址不一样,别的都一样的
获取电话上的
private void getPhoneContacts() {
ContentResolver resolver = this.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME,
Phone.NUMBER },
Phone.DISPLAY_NAME + "=?" + " AND " + Phone.TYPE + "='"
+ Phone.TYPE_MOBILE + "'", new String[] { "name" }, null);
HashMap<String, String> map = new HashMap<String, String>();
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
String number = phoneCursor.getString(2);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(number))
continue;
// 得到联系人名称
String username = phoneCursor.getString(1);
map.put(username, number);
}
phoneCursor.close();
}
System.out.println("phone"+map);
Log.i("abc", map.toString());
Toast.makeText(this, "222"+map.toString(),1 ).show();
}
获取sim卡上的
private void getSimContacts() {
ContentResolver resolver = this.getContentResolver();
Uri uri = Uri.parse("content://icc/adn");
// 获取手机联系人
Cursor phoneCursor = resolver.query(uri,
new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME,
Phone.NUMBER },
Phone.DISPLAY_NAME + "=?" + " AND " + Phone.TYPE + "='"
+ Phone.TYPE_MOBILE + "'", new String[] { "name" }, null);
HashMap<String, String> map = new HashMap<String, String>();
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
String number = phoneCursor.getString(2);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(number))
continue;
// 得到联系人名称
String username = phoneCursor.getString(1);
map.put(username, number);
}
phoneCursor.close();
}
System.out.println("sim"+map);
Log.i("abc", map.toString());
Toast.makeText(this, map.toString(),1 ).show();
}
3 在手机上更能体现出效果,结果不太好演示,但是是可以获取信息的