android根据电话号码查询联系人名称,导出通讯录所有联系人的方法
android根据电话号码查询联系人名称,导出通讯录所有联系人的方法
1 /*
2 * 根据电话号码取得联系人姓名
3 */
4 public static String getContactNameByPhoneNumber(Context context, String address) {
5 String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
6 ContactsContract.CommonDataKinds.Phone.NUMBER };
7
8 // 将自己添加到 msPeers 中
9 Cursor cursor = context.getContentResolver().query(
10 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
11 projection, // Which columns to return.
12 ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
13 + address + "'", // WHERE clause.
14 null, // WHERE clause value substitution
15 null); // Sort order.
16
17 if (cursor == null) {
18 Log.d(TAG, "getPeople null");
19 return null;
20 }
21 for (int i = 0; i < cursor.getCount(); i++) {
22 cursor.moveToPosition(i);
23
24 // 取得联系人名字
25 int nameFieldColumnIndex = cursor
26 .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
27 String name = cursor.getString(nameFieldColumnIndex);
28 return name;
29 }
30 return null;
31 }
32
33 /**
34 * 获取所有联系人内容
35 * @param context
36 * @param address
37 * @return
38 */
39 public static String getContacts(Context context) {
40 StringBuilder sb = new StringBuilder();
41
42 ContentResolver cr = context.getContentResolver();
43 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
44 null, null, null);
45
46 if (cursor.moveToFirst()) {
47 do {
48 String contactId = cursor.getString(cursor
49 .getColumnIndex(ContactsContract.Contacts._ID));
50 String name = cursor
51 .getString(cursor
52 .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
53 //第一条不用换行
54 if(sb.length() == 0){
55 sb.append(name);
56 }else{
57 sb.append("\n" + name);
58 }
59
60 Cursor phones = cr.query(
61 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
62 null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
63 + " = " + contactId, null, null);
64 while (phones.moveToNext()) {
65 String phoneNumber = phones
66 .getString(phones
67 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
68 // 添加Phone的信息
69 sb.append("\t").append(phoneNumber);
70
71 }
72 phones.close();
73
74 } while (cursor.moveToNext());
75 }
76 cursor.close();
77 return sb.toString();
2 * 根据电话号码取得联系人姓名
3 */
4 public static String getContactNameByPhoneNumber(Context context, String address) {
5 String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
6 ContactsContract.CommonDataKinds.Phone.NUMBER };
7
8 // 将自己添加到 msPeers 中
9 Cursor cursor = context.getContentResolver().query(
10 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
11 projection, // Which columns to return.
12 ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
13 + address + "'", // WHERE clause.
14 null, // WHERE clause value substitution
15 null); // Sort order.
16
17 if (cursor == null) {
18 Log.d(TAG, "getPeople null");
19 return null;
20 }
21 for (int i = 0; i < cursor.getCount(); i++) {
22 cursor.moveToPosition(i);
23
24 // 取得联系人名字
25 int nameFieldColumnIndex = cursor
26 .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
27 String name = cursor.getString(nameFieldColumnIndex);
28 return name;
29 }
30 return null;
31 }
32
33 /**
34 * 获取所有联系人内容
35 * @param context
36 * @param address
37 * @return
38 */
39 public static String getContacts(Context context) {
40 StringBuilder sb = new StringBuilder();
41
42 ContentResolver cr = context.getContentResolver();
43 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
44 null, null, null);
45
46 if (cursor.moveToFirst()) {
47 do {
48 String contactId = cursor.getString(cursor
49 .getColumnIndex(ContactsContract.Contacts._ID));
50 String name = cursor
51 .getString(cursor
52 .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
53 //第一条不用换行
54 if(sb.length() == 0){
55 sb.append(name);
56 }else{
57 sb.append("\n" + name);
58 }
59
60 Cursor phones = cr.query(
61 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
62 null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
63 + " = " + contactId, null, null);
64 while (phones.moveToNext()) {
65 String phoneNumber = phones
66 .getString(phones
67 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
68 // 添加Phone的信息
69 sb.append("\t").append(phoneNumber);
70
71 }
72 phones.close();
73
74 } while (cursor.moveToNext());
75 }
76 cursor.close();
77 return sb.toString();
78 }
大自然,飘然的风,QQ群: python技术交流群:453879716,人工智能深度学习群:251088643
golang技术交流群:316397059,vuejs技术交流群:458915921 囤币一族:621258209,有兴趣的可以加入
微信公众号: 心禅道(xinchandao)投资论道
golang技术交流群:316397059,vuejs技术交流群:458915921 囤币一族:621258209,有兴趣的可以加入
微信公众号: 心禅道(xinchandao)投资论道
分类:
android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?