欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android获取手机联系人信息

程序员文章站 2024-03-05 16:01:01
android如何获取手机联系人信息,本文为大家揭晓。 获取手机联系人信息步骤: 1、获取 contentresolver contentresolver...

android如何获取手机联系人信息,本文为大家揭晓。

获取手机联系人信息步骤:

1、获取 contentresolver

contentresolver resolver = getcontentresolver();

2、resolver.query(*)查询信息

查询手机联系人的uri:contactscontract.rawcontacts.content_uri
查询手机联系人手机号的uri:contactscontract.commondatakinds.phone.content_uri
查询联系人邮箱的uri:contactscontract.commondatakinds.email.content_uri

3、根据联系人名字的_id查询对应的手机号和邮箱

在手机号和邮箱查询中找到raw_contact_id与联系人的_id相等的条目

主要代码如下:

mainactivity.java

package com.noonecode.contentresolvercontacts;

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.os.bundle;
import android.provider.contactscontract.commondatakinds.email;
import android.provider.contactscontract.commondatakinds.phone;
import android.provider.contactscontract.rawcontacts;
import android.widget.listview;
import android.widget.simpleadapter;

public class mainactivity extends activity {

 private listview mlvshow;
 private list<map<string, string>> datalist;
 private simpleadapter adapter;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 mlvshow = (listview) findviewbyid(r.id.lv_show);
 datalist = getdatalist();
 adapter = new simpleadapter(this, datalist, r.layout.simple_contacts_item//
  , new string[] { "name", "number", "email" }//
  , new int[] { r.id.tv_name, r.id.tv_number, r.id.tv_email });
 mlvshow.setadapter(adapter);
 }

 /**
 * 加载数据
 * 
 * @return
 */
 private list<map<string, string>> getdatalist() {
 list<map<string, string>> list = new arraylist<map<string, string>>();
 contentresolver resolver = getcontentresolver();
 cursor cursor = resolver.query(rawcontacts.content_uri,
  new string[] { rawcontacts._id, rawcontacts.display_name_primary }//
  , null, null, null);
 while (cursor.movetonext()) {
  map<string, string> map = new hashmap<string, string>();
  string id = cursor.getstring(cursor.getcolumnindex(rawcontacts._id));// "_id"
  string name = cursor.getstring(cursor.getcolumnindex(rawcontacts.display_name_primary));// "display_name"
  map.put("name", name);

  // 联系人号码
  cursor phonecursor = resolver.query(phone.content_uri//
   , new string[] { phone.number }// "data1"
   , "raw_contact_id=?", new string[] { id }, null);
  if (phonecursor.movetonext()) {
  string number = phonecursor.getstring(phonecursor.getcolumnindex(phone.number));
  map.put("number", number);
  }

  // 联系人邮箱
  cursor emailcursor = resolver.query(email.content_uri//
   , new string[] { email.address}// "data1"
   , "raw_contact_id=?", new string[] { id }, null);
  if (emailcursor.movetonext()) {
  string email = emailcursor.getstring(emailcursor.getcolumnindex(email.address));
  map.put("email", email);
  }
  list.add(map);
 }
 return list;
 }
}

主布局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"
 tools:context="com.noonecode.contentresolvercontacts.mainactivity" >

 <listview
 android:id="@+id/lv_show"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

</relativelayout>

simple_contacts_item.xml

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="10dp" >

 <textview
 android:id="@+id/tv_name"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="name"
 android:textsize="22sp" />

 <textview
 android:id="@+id/tv_number"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/tv_name"
 android:text="number" />

 <textview
 android:id="@+id/tv_email"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/tv_name"
 android:layout_marginleft="10dp"
 android:layout_torightof="@id/tv_number"
 android:text="email" />

</relativelayout>

读取联系人的权限:

<uses-permission android:name="android.permission.read_contacts"/>

最终效果图:

Android获取手机联系人信息

注意:

注意检查是否赋予了应用读取联系人的权限;
本例在主线程中读取联系人信息,效率不高,如有效率要求,请自行寻找其他方法。

源码下载地址: http://xiazai.jb51.net/201610/yuanma/androidcontactsdemo(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。