发布于 2016-05-11 22:25:40 | 105 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程基于Contacts读取联系人的方法,实例分析了Contacts读取的实现方法及权限设置方法,并附带了完整实例供读者下载参考,需要的朋友可以参考下

本文实例讲述了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" />

运行结果:

 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务