Android编程基于Contacts读取联系人的方法(附demo源码)
作者:阳光岛主 时间:2023-11-22 06:05:39
本文实例讲述了Android编程基于Contacts读取联系人的方法。分享给大家供大家参考,具体如下:
Android Contacts简介:
这里介绍安卓通讯录数据库。包括Android使用Contacts访问SQLite的基本知识,并了解Android SQLite和Contacts的更多信息。谷歌改变了从版本1到版本2的Contacts数据库。下面加以简单介绍。
Contacts 读取代码:
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程序设计有所帮助。
标签:Android,Contacts
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java基础知识之FileInputStream流的使用
2021-07-05 00:30:24
![](https://img.aspxhome.com/file/2023/6/104036_0s.jpg)
深入了解ViewPager2的使用
2021-07-06 17:26:26
![](https://img.aspxhome.com/file/2023/3/100653_0s.gif)
异常try catch的常见四类方式(案例代码)
2023-11-10 23:18:00
SpringCloud Eureka实现服务注册与发现
2021-10-25 11:41:58
![](https://img.aspxhome.com/file/2023/9/63499_0s.png)
kettle中使用js调用java类的方法
2022-05-09 00:06:31
Android实现掷骰子效果
2022-06-16 01:09:16
![](https://img.aspxhome.com/file/2023/6/103926_0s.gif)
springboot如何读取配置文件到静态工具类
2023-11-28 04:44:54
![](https://img.aspxhome.com/file/2023/6/60036_0s.png)
java面向对象编程类的内聚性分析
2022-02-24 23:43:00
c#简单工厂、工厂方法与抽象工厂的区别分析
2021-11-30 04:39:47
探讨Android 的屏幕滚动操作不如 iPhone 流畅顺滑的原因
2023-04-05 09:05:33
Android新特性页面之ViewPager拖拽到最后一页再拖拽打开其他Activity(三种方法)
2021-11-30 03:05:40
利用json2POJO with Lombok 插件自动生成java类的操作
2023-07-12 09:31:28
![](https://img.aspxhome.com/file/2023/8/69718_0s.jpg)
C#实现汉字转汉语拼音的示例代码
2022-08-05 14:04:53
![](https://img.aspxhome.com/file/2023/8/129738_0s.png)
微信Android热更新Tinker使用详解(星空武哥)
2023-06-08 04:08:17
![](https://img.aspxhome.com/file/2023/0/138630_0s.png)
JAVA实现PDF转HTML文档的示例代码
2021-10-18 12:47:00
![](https://img.aspxhome.com/file/2023/9/125629_0s.jpg)
Java类加载初始化的过程及顺序
2021-12-09 16:12:46
![](https://img.aspxhome.com/file/2023/8/81928_0s.png)
spring 中事务注解@Transactional与trycatch的使用
2022-08-28 18:26:07
Java 如何将网络资源url转化为File文件
2023-05-28 09:23:37
springboot 如何设置端口号和添加项目名
2022-01-11 07:31:12
![](https://img.aspxhome.com/file/2023/5/78335_0s.png)
C# Winform中如何绘制动画示例详解
2022-03-28 13:26:26