android 关于联系人列表的一个类

 
   
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;

/*****
* 需要权限android.permission.READ_CONTACTS 获取联系人列表返回是一个map注意key是号码防止同一个人多个号码
* map遍历使用方法 Map map = new HashMap();   Iterator iter =
* map.entrySet().iterator();   while (iter.hasNext()) {   Map.Entry entry =
* (Map.Entry) iter.next();   Object key = entry.getKey();   Object val =
* entry.getValue();   }
*****
*/
public class GetContactList {

public static Map<String, String> getContact(Context c) {
Map<String, String> map = new HashMap<String, String>();

// 获得所有的联系人
Cursor cur = c.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// 循环遍历
if (cur.moveToFirst()) {
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do {
// 获得联系人的ID号
String contactId = cur.getString(idColumn);
// 获得联系人姓名
String disPlayName = cur.getString(displayNameColumn);
// 查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cur
.getInt(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (phoneCount > 0) {
// 获得联系人的电话号码
Cursor phones = c.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (phones.moveToFirst()) {
do {
// 遍历所有的电话号码
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// 以电话号码为关键字
map.put(phoneNumber, disPlayName);
} while (phones.moveToNext());
}
}
} while (cur.moveToNext());
}
cur.close();
return map;
}

public static class DialogListContact {
private OnSelectOk onSelectOk;
private Context mContext;
private boolean mMulti;
private boolean load = true;

public DialogListContact(Context c, final OnSelectOk onSelectOk,
boolean multi) {
this.onSelectOk = onSelectOk;
this.mContext = c;
this.mMulti = multi;

final ProgressDialog mProgressDialog = new ProgressDialog(mContext);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("正在获取联系人列表...");
mProgressDialog.setCancelable(true);
mProgressDialog
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
//防止在被取消后 线程会继续运行显示
load = false;
}
});
mProgressDialog.show();

getContactForThread(new Handler() {
@Override
public void handleMessage(Message msg) {
if (load) {
mProgressDialog.dismiss();
@SuppressWarnings("unchecked")
Map<String, String> contactList = (Map<String, String>) msg.obj;
String[] items = getItems(contactList);
String[] phones = contactList.keySet().toArray(
new String[0]);

if (mMulti) {
multiDialog(items, phones);
} else {
singleDialog(items, phones);
}
}
}
});

}

public void getContactForThread(final Handler handler) {
new Thread(new Runnable() {

@Override
public void run() {
Map<String, String> contactList = getContact(mContext);
Message msg = new Message();
msg.obj = contactList;
handler.sendMessage(msg);
}
}).start();
}

// 单选,其实就是一个列表
private void singleDialog(String[] items, final String[] phones) {
new AlertDialog.Builder(mContext).setTitle("请选择电话号码")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (onSelectOk != null) {
onSelectOk.onSelectOk(phones[which]);
}
}
}).setNegativeButton("取消", null)
.setIcon(android.R.drawable.ic_dialog_info).show();
}

private void multiDialog(String[] items, final String[] phones) {
final Map<String, String> selectList = new HashMap<String, String>();
new AlertDialog.Builder(mContext)
.setTitle("请选择电话号码")
.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
if (isChecked) {
String select = phones[which];
selectList.put(select, select);
} else {
String select = phones[which];
selectList.remove(select);
}
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
if (onSelectOk != null) {
onSelectOk
.onSelectOk(getMultiResult(selectList));
}
}
}).setNegativeButton("取消", null)
.setIcon(android.R.drawable.ic_dialog_info).show();

}

public interface OnSelectOk {
public void onSelectOk(String result);
}

private String[] getItems(Map<String, String> map) {
List<String> ls = new ArrayList<String>();
Iterator<Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
.next();
String key = entry.getKey();
String val = entry.getValue();
ls.add(key + "(" + val + ")");
}
return ls.toArray(new String[0]);
}

private String getMultiResult(Map<String, String> map) {
String result = "";
Iterator<Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
.next();
result += entry.getValue() + ";";
}
return result;
}
}

}



 

android 关于联系人列表的一个类

android 关于联系人列表的一个类

你可能感兴趣的:(android)