欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程基于Contacts讀取聯(lián)系人的方法(附demo源碼)

 更新時間:2015年12月14日 14:34:16   作者:陽光島主  
這篇文章主要介紹了Android編程基于Contacts讀取聯(lián)系人的方法,實例分析了Contacts讀取的實現(xiàn)方法及權(quán)限設(shè)置方法,并附帶了完整實例供讀者下載參考,需要的朋友可以參考下

本文實例講述了Android編程基于Contacts讀取聯(lián)系人的方法。分享給大家供大家參考,具體如下:

Android Contacts簡介:

這里介紹安卓通訊錄數(shù)據(jù)庫。包括Android使用Contacts訪問SQLite的基本知識,并了解Android SQLite和Contacts的更多信息。谷歌改變了從版本1到版本2的Contacts數(shù)據(jù)庫。下面加以簡單介紹。

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);  // 獲取手機聯(lián)系人 
  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 權(quán)限

記得在AndroidManifest.xml中加入android.permission.READ_CONTACTS這個permission

復(fù)制代碼 代碼如下:
<uses-permission android:name="android.permission.READ_CONTACTS" />

運行結(jié)果:

 

示例代碼點擊此處本站下載。

希望本文所述對大家Android程序設(shè)計有所幫助。

您可能感興趣的文章:

相關(guān)文章

最新評論