『壹』 android開發怎樣獲取通訊錄聯系人信息
要獲取通訊錄的聯系人信息,只有通過系統聯系人應用的內容提供者,才能訪問到聯系人資料庫,下面有一個代碼演示
代碼演示
java">privateArrayList<HashMap<String,String>>readContact(){
//首先,從raw_contacts中讀取聯系人的id("contact_id")
//其次,根據contact_id從data表中查詢出相應的電話號碼和聯系人名稱
//然後,根據mimetype來區分哪個是聯系人,哪個是電話號碼
UrirawContactsUri=Uri
.parse("content://com.android.contacts/raw_contacts");
UridataUri=Uri.parse("content://com.android.contacts/data");
ArrayList<HashMap<String,String>>list=newArrayList<>();
//從raw_contacts中讀取聯系人的id("contact_id")
CursorrawContactsCursor=getContentResolver().query(rawContactsUri,
newString[]{"contact_id"},null,null,null);
if(rawContactsCursor!=null){
while(rawContactsCursor.moveToNext()){
StringcontactId=rawContactsCursor.getString(0);
//System.out.println(contactId);
//根據contact_id從data表中查詢出相應的電話號碼和聯系人名稱,實際上查詢的是視圖view_data
CursordataCursor=getContentResolver().query(dataUri,
newString[]{"data1","mimetype"},"contact_id=?",
newString[]{contactId},null);
if(dataCursor!=null){
HashMap<String,String>map=newHashMap<String,String>();
while(dataCursor.moveToNext()){
Stringdata1=dataCursor.getString(0);
Stringmimetype=dataCursor.getString(1);
//System.out.println(contactId+";"+data1+";"
//+mimetype);
if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
map.put("phone",data1);
}elseif("vnd.android.cursor.item/name"
.equals(mimetype)){
map.put("name",data1);
}
}
list.add(map);
dataCursor.close();
}
}
rawContactsCursor.close();
}
returnlist;
}
手機聯系人圖片:
『貳』 android開發怎樣獲取通訊錄聯系人信息
在Android開發中,我經常會遇到需要獲取手機通訊錄聯系人信息,Android手機的通訊錄聯系人全部都存在系統的資料庫中,如果須要獲得通訊里聯系人的信息就須要訪問系統的資料庫,才能將信息獲取出來。
下面直接貼出代碼供大家參考:
程序文件java代碼:
import java.io.InputStream;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ContactsActivity extends ListActivity {
Context mContext = null;
/**獲取庫Phon表欄位**/
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };
/**聯系人顯示名稱**/
private static final int PHONES_DISPLAY_NAME_INDEX = 0;
/**電話號碼**/
private static final int PHONES_NUMBER_INDEX = 1;
/**頭像ID**/
private static final int PHONES_PHOTO_ID_INDEX = 2;
/**聯系人的ID**/
private static final int PHONES_CONTACT_ID_INDEX = 3;
/**聯系人名稱**/
private ArrayList<String> mContactsName = new ArrayList<String>();
/**聯系人頭像**/
private ArrayList<String> mContactsNumber = new ArrayList<String>();
/**聯系人頭像**/
private ArrayList<Bitmap> mContactsPhonto = new ArrayList<Bitmap>();
ListView mListView = null;
MyListAdapter myAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
mContext = this;
mListView = this.getListView();
/**得到手機通訊錄聯系人信息**/
getPhoneContacts();
myAdapter = new MyListAdapter(this);
setListAdapter(myAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
//調用系統方法撥打電話
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:" + mContactsNumber.get(position)));
startActivity(dialIntent);
}
});
super.onCreate(savedInstanceState);
}
/**得到手機通訊錄聯系人信息**/
private void getPhoneContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 獲取手機聯系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
//得到手機號碼
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
//當手機號碼為空的或者為空欄位 跳過當前循環
if (TextUtils.isEmpty(phoneNumber))
continue;
//得到聯系人名稱
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
//得到聯系人ID
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
//得到聯系人頭像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
//得到聯系人頭像Bitamp
Bitmap contactPhoto = null;
//photoid 大於0 表示聯系人有頭像 如果沒有給此人設置頭像則給他一個默認的
if(photoid > 0 ) {
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}else {
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
}
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
}
phoneCursor.close();
}
}
/**得到手機SIM卡聯系人人信息**/
private void getSIMContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 獲取Sims卡聯系人
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
// 得到手機號碼
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
// 當手機號碼為空的或者為空欄位 跳過當前循環
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到聯系人名稱
String contactName = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
//Sim卡中沒有聯系人頭像
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
}
phoneCursor.close();
}
}
class MyListAdapter extends BaseAdapter {
public MyListAdapter(Context context) {
mContext = context;
}
public int getCount() {
//設置繪制數量
return mContactsName.size();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iamge = null;
TextView title = null;
TextView text = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.colorlist, null);
iamge = (ImageView) convertView.findViewById(R.id.color_image);
title = (TextView) convertView.findViewById(R.id.color_title);
text = (TextView) convertView.findViewById(R.id.color_text);
}
//繪制聯系人名稱
title.setText(mContactsName.get(position));
//繪制聯系人號碼
text.setText(mContactsNumber.get(position));
//繪制聯系人頭像
iamge.setImageBitmap(mContactsPhonto.get(position));
return convertView;
}
}
}