android2.1读取手机通讯录功能和以前版本不太一样,看代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
/**
* android 2.1以上读取手机通讯录,一个人有多个号码时用|分隔
* @author acer
*
*/
public class PhoneActivity extends Activity {
LinearLayout mLinearLayout;
ListView mLstView;
ArrayList<Map<String, String>> listData ;
static final String NAME="name";
static final String NUMBER="number";
String jsondata = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mLinearLayout.setBackgroundColor(Color.BLACK);
mLstView = new ListView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
mLstView.setBackgroundColor(Color.BLACK);
mLinearLayout.addView(mLstView, params);
setContentView(mLinearLayout);
listData = new ArrayList<Map<String, String>>();
// 读取通讯录
jsondata = "[";
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, select, null, null);
startManagingCursor(cur);
while (cur.moveToNext()) {
JSONObject phonedate = new JSONObject();
Map<String, String> mpMap = new HashMap<String, String>();
String _id = cur.getString(cur.getColumnIndex("_id"));
Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + _id, null, null);
// 处理多个号码的情况
String phoneNumbers = "";
while(pcur.moveToNext()){
String strPhoneNumberString = pcur.getString(pcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumbers += strPhoneNumberString + "|";
}
pcur.close();
String nameString = cur.getString(cur.getColumnIndex("display_name"));
mpMap.put(NAME, nameString);
mpMap.put(NUMBER, phoneNumbers);
listData.add(mpMap);
try {
phonedate.put("phone_id", _id);
phonedate.put("name", nameString);
phonedate.put("number", phoneNumbers);
} catch (JSONException e) {
Log.e("PHONE", "构建JSON数据出现异常", e);
e.printStackTrace();
}
}
cur.close();
// 建立一个适配器去查询数据
ListAdapter adapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_2, new String[]{NAME,NUMBER}, new int[] {android.R.id.text1,android.R.id.text2});
mLstView.setAdapter(adapter);
/* 为m_ListView视图添加setOnItemSelectedListener监听 */
mLstView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
DisplayToast("滚动到第" + Long.toString(arg0.getSelectedItemId()) + "项");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
/* 为m_ListView视图添加setOnItemClickListener监听 */
mLstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// 于对选中的项进行处理
DisplayToast("选中了第" + Integer.toString(arg2 + 1) + "项");
}
});
}
/* 显示Toast */
public void DisplayToast(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}