android获取联系人示例分享
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中加入android.permission.read_contacts这个permission
<uses-permission android:name="android.permission.read_contacts" />
上一篇: android动态加载布局文件示例
下一篇: 生湿疹吃什么好,今天介绍给大家