当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程基于Contacts读取联系人的方法(附demo源码)

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

引用男体,河南大学选课系统,林家志弦

本文实例讲述了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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网