Android Contacts(一)—— 读取联系人
2014-02-18 17:34
387 查看
Introduction To Android Contacts
Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See the AndroidSQLite and Cursor Article for more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with
accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.
Contacts 读取代码:
[java] view
plaincopyprint?
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" />
运行结果:
转自:http://blog.csdn.net/ithomer/article/details/7328590
相关文章推荐
- 读取手机联系人和SIM卡联系人
- android中判断sim卡状态和读取联系人资料的方法
- 读取系统联系人
- Android Contacts(一)—— 读取联系人
- android_73_读取联系人
- Android程序如何读取手机联系人
- Android关于手机数据读取联系人数据
- CursorLoader加载器读取手机联系人的简单记录(17)
- android读取通讯录联系人问题
- android中判断sim卡状态和读取联系人资料的方法
- Android读取短信和联系人
- Android读取联系人信息和选择联系人信息
- [android开发]ContentResolver读取手机联系人信息
- Android联系人读取操作笔记
- Android联系人读取操作笔记
- Android使用READ_CONTACTS读取手机联系人
- Android SMS —— 读取短信 联系人
- 简单的读取 存储联系人的DEMO~
- Android软件开发之获取通讯录联系人信息 + android联系人信息的存储结构 + Android联系人读取操作笔记
- Android根据联系人姓名首字符顺序读取通讯录