Android編程基于Contacts讀取聯(lián)系人的方法(附demo源碼)
本文實(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); // 獲取手機(jī)聯(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這個(gè)permission
運(yùn)行結(jié)果:

示例代碼點(diǎn)擊此處本站下載。
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android webview和js互相調(diào)用實(shí)現(xiàn)方法
這篇文章主要介紹了 Android webview和js互相調(diào)用實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android調(diào)試華為和魅族手機(jī)logcat不顯示的問題
今天小編就為大家分享一篇關(guān)于Android調(diào)試華為和魅族手機(jī)logcat不顯示的問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
android教程之使用popupwindow創(chuàng)建菜單示例
這篇文章主要介紹了android使用popupwindow創(chuàng)建菜單的示例,需要的朋友可以參考下2014-02-02
Android動畫效果之自定義ViewGroup添加布局動畫(五)
這篇文章主要介紹了Android動畫效果之自定義ViewGroup添加布局動畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android開發(fā)微信小程序路由跳轉(zhuǎn)方式
這篇文章主要為大家介紹了Android開發(fā)微信小程序路由跳轉(zhuǎn)方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04

