Android开发之获取手机通讯录
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android开发之获取手机通讯录,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4045字,纯文字阅读大概需要6分钟。
内容图文
![Android开发之获取手机通讯录](/upload/InfoBanner/zyjiaocheng/1121/44ea1c8947b1428ea0bfbdabbf832c48.jpg)
获取手机通讯录是Android最常用的小功能,今天自学到了,记下来,主要是通过系统自带的内容提供者提供的数据,我们使用内容接收者获取相应的数据到cursor中,然后获取对应data表中的字段,相关字段代表什么含义,只能自己去查了。
下面是手机通讯录列表的代码,仅供参考:
package com.andy.phonecontact; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; /** * @author Zhang,Tianyou * @version 2014年11月18日 下午10:00:46 */ public class MainActivity extends Activity { private ListView select_contact; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); select_contact = (ListView) findViewById(R.id.select_contact); List<Map<String, String>> data = getContactInfo(); select_contact.setAdapter(new SimpleAdapter(MainActivity.this, data, R.layout.contact_item_view, new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone })); } /** * 读取手机里的联系人信息 * * @return */ private List<Map<String, String>> getContactInfo() { // 把所有的联系人放到list List<Map<String, String>> list = new ArrayList<Map<String, String>>(); // 得到一个内容解析器 ContentResolver resolver = getContentResolver(); // 获取联系人表对应的内容提供者url raw_contacts表和data表 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); // 获取contact_id 获取联系人id while (cursor.moveToNext()) { String contact_id = cursor.getString(0); if (contact_id != null) { // 具体的某个联系人 Map<String, String> map = new HashMap<String, String>(); // 如果不为空 查询对应data表的联系人信息 Cursor dataCursor = resolver.query(datauri, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contact_id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); System.out.println("data1 ==" + data1 + "mimetype == " + mimetype); if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { System.out.println("电话:" + data1); map.put("phone", data1); } else if ("vnd.android.cursor.item/name".equals(mimetype)) { System.out.println("姓名:" + data1); map.put("name", data1); } } list.add(map); // 释放游标 dataCursor.close(); } } cursor.close(); return list; } }
2 布局文件 在activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.andy.phonecontact.MainActivity" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/select_contact" /> </RelativeLayout>
3 ListView填充布局contact_item_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" android:textColor="#ff0000" android:textSize="22sp" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@android:drawable/ic_menu_call" android:text="5558" android:textColor="#000000" android:textSize="22sp" /> </LinearLayout>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
原文:http://blog.csdn.net/fengshizty/article/details/41259595
内容总结
以上是互联网集市为您收集整理的Android开发之获取手机通讯录全部内容,希望文章能够帮你解决Android开发之获取手机通讯录所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。