服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android编程基于Contacts读取联系人的方法(附demo源码)

Android编程基于Contacts读取联系人的方法(附demo源码)

2021-04-21 17:30阳光岛主 Android

这篇文章主要介绍了Android编程基于Contacts读取联系人的方法,实例分析了Contacts读取的实现方法及权限设置方法,并附带了完整实例供读者下载参考,需要的朋友可以参考下

本文实例讲述了Android编程基于Contacts读取联系人的方法。分享给大家供大家参考,具体如下:

Android Contacts简介:

这里介绍安卓通讯录数据库。包括Android使用Contacts访问SQLite的基本知识,并了解Android SQLite和Contacts的更多信息。谷歌改变了从版本1到版本2的Contacts数据库。下面加以简单介绍。

Contacts 读取代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.homer.phone;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class phoneRead extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  showListView();
 }
 private void showListView(){
  ListView listView = new ListView(this);
  ArrayList<HashMap<String, String>> list = getPeopleInPhone2();
  SimpleAdapter adapter = new SimpleAdapter(
         this,
         list,
         android.R.layout.simple_list_item_2,
         new String[] {"peopleName", "phoneNum"},
         new int[]{android.R.id.text1, android.R.id.text2}
        );
  listView.setAdapter(adapter);
  setContentView(listView);
 }
 private ArrayList<HashMap<String, String>> getPeopleInPhone2(){
  ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
  Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);  // 获取手机联系人
  while (cursor.moveToNext()) {
   HashMap<String, String> map = new HashMap<String, String>();
   int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME); // people name
   int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER);   // phone number
   String strPeopleName = cursor.getString(indexPeopleName);
   String strPhoneNum = cursor.getString(indexPhoneNum);
   map.put("peopleName", strPeopleName);
   map.put("phoneNum", strPhoneNum);
   list.add(map);
  }
  if(!cursor.isClosed()){
   cursor.close();
   cursor = null;
  }
  return list;
 }
}

AndroidManifest.xml 权限

记得在AndroidManifest.xml中加入android.permission.READ_CONTACTS这个permission

复制代码 代码如下:
<uses-permission android:name="android.permission.READ_CONTACTS" />

 

运行结果:

 Android编程基于Contacts读取联系人的方法(附demo源码)

示例代码点击此处本站下载

希望本文所述对大家Android程序设计有所帮助。

延伸 · 阅读

精彩推荐